-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): Support totalCount on BAPI responses
The backend clerkClient will return a `totalCount: number` in the responses for BAPI responses: - with `total_count` prop of response - with an array of objects as `data` prop of response The `totalCount` is NOT included in: - single resource responses - error responses
- Loading branch information
Showing
6 changed files
with
117 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
'@clerk/backend': major | ||
--- | ||
|
||
Expose `totalCount` from `@clerk/backend` client responses for responses | ||
containing pagination information or for responses with type `{ data: object[] }`. | ||
|
||
|
||
Example: | ||
```typescript | ||
import { Clerk } from '@clerk/backend' | ||
|
||
const clerkClient = Clerk({ secretKey: '...'}); | ||
|
||
// current | ||
const { data } = await clerkClient.organizations.getOrganizationList(); | ||
console.log('totalCount: ', data.length); | ||
|
||
// new | ||
const { data, totalCount } = await clerkClient.organizations.getOrganizationList(); | ||
console.log('totalCount: ', totalCount); | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import emailJson from '../fixtures/responses/email.json'; | |
import userJson from '../fixtures/responses/user.json'; | ||
import runtime from '../runtime'; | ||
import { assertErrorResponse, assertResponse } from '../util/assertResponse'; | ||
import { jsonError, jsonNotOk, jsonOk } from '../util/mockFetch'; | ||
import { jsonError, jsonNotOk, jsonOk, jsonPaginatedOk } from '../util/mockFetch'; | ||
import { createBackendApiClient } from './factory'; | ||
|
||
export default (QUnit: QUnit) => { | ||
|
@@ -30,14 +30,15 @@ export default (QUnit: QUnit) => { | |
const response = await apiClient.users.getUser('user_deadbeef'); | ||
|
||
assertResponse(assert, response); | ||
const { data: payload } = response; | ||
const { data: payload, totalCount } = response; | ||
|
||
assert.equal(payload.firstName, 'John'); | ||
assert.equal(payload.lastName, 'Doe'); | ||
assert.equal(payload.emailAddresses[0].emailAddress, '[email protected]'); | ||
assert.equal(payload.phoneNumbers[0].phoneNumber, '+311-555-2368'); | ||
assert.equal(payload.externalAccounts[0].emailAddress, '[email protected]'); | ||
assert.equal(payload.publicMetadata.zodiac_sign, 'leo'); | ||
assert.equal(totalCount, undefined); | ||
|
||
assert.ok( | ||
fakeFetch.calledOnceWith('https://api.clerk.test/v1/users/user_deadbeef', { | ||
|
@@ -57,14 +58,15 @@ export default (QUnit: QUnit) => { | |
|
||
const response = await apiClient.users.getUserList({ offset: 2, limit: 5 }); | ||
assertResponse(assert, response); | ||
const { data: payload } = response; | ||
const { data: payload, totalCount } = response; | ||
|
||
assert.equal(payload[0].firstName, 'John'); | ||
assert.equal(payload[0].lastName, 'Doe'); | ||
assert.equal(payload[0].emailAddresses[0].emailAddress, '[email protected]'); | ||
assert.equal(payload[0].phoneNumbers[0].phoneNumber, '+311-555-2368'); | ||
assert.equal(payload[0].externalAccounts[0].emailAddress, '[email protected]'); | ||
assert.equal(payload[0].publicMetadata.zodiac_sign, 'leo'); | ||
assert.equal(totalCount, 1); | ||
|
||
assert.ok( | ||
fakeFetch.calledOnceWith('https://api.clerk.test/v1/users?offset=2&limit=5', { | ||
|
@@ -92,10 +94,11 @@ export default (QUnit: QUnit) => { | |
'{"from_email_name":"foobar123","email_address_id":"[email protected]","body":"this is a test","subject":"this is a test"}'; | ||
const response = await apiClient.emails.createEmail(body); | ||
assertResponse(assert, response); | ||
const { data: payload } = response; | ||
const { data: payload, totalCount } = response; | ||
|
||
assert.equal(JSON.stringify(payload.data), '{}'); | ||
assert.equal(payload.id, 'ema_2PHa2N3bS7D6NPPQ5mpHEg0waZQ'); | ||
assert.equal(totalCount, undefined); | ||
|
||
assert.ok( | ||
fakeFetch.calledOnceWith('https://api.clerk.test/v1/emails', { | ||
|
@@ -110,6 +113,36 @@ export default (QUnit: QUnit) => { | |
); | ||
}); | ||
|
||
test('executes a successful backend API request for a paginated response', async assert => { | ||
fakeFetch = sinon.stub(runtime, 'fetch'); | ||
fakeFetch.onCall(0).returns(jsonPaginatedOk([userJson], 3)); | ||
|
||
const response = await apiClient.users.getUserList({ offset: 2, limit: 5 }); | ||
assertResponse(assert, response); | ||
const { data: payload, totalCount } = response; | ||
|
||
assert.equal(payload[0].firstName, 'John'); | ||
assert.equal(payload[0].lastName, 'Doe'); | ||
assert.equal(payload[0].emailAddresses[0].emailAddress, '[email protected]'); | ||
assert.equal(payload[0].phoneNumbers[0].phoneNumber, '+311-555-2368'); | ||
assert.equal(payload[0].externalAccounts[0].emailAddress, '[email protected]'); | ||
assert.equal(payload[0].publicMetadata.zodiac_sign, 'leo'); | ||
// payload.length is different from response total_count to check that totalCount use the total_count from response | ||
assert.equal(payload.length, 1); | ||
assert.equal(totalCount, 3); | ||
|
||
assert.ok( | ||
fakeFetch.calledOnceWith('https://api.clerk.test/v1/users?offset=2&limit=5', { | ||
method: 'GET', | ||
headers: { | ||
Authorization: 'Bearer deadbeef', | ||
'Content-Type': 'application/json', | ||
'Clerk-Backend-SDK': '@clerk/backend', | ||
}, | ||
}), | ||
); | ||
}); | ||
|
||
test('executes a successful backend API request to create a new resource', async assert => { | ||
fakeFetch = sinon.stub(runtime, 'fetch'); | ||
fakeFetch.onCall(0).returns(jsonOk(userJson)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters