Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9810482eef | ||
|
|
944e84802c |
+1
-2
@@ -53,9 +53,8 @@
|
||||
"build:esm": "tsc --project tsconfig.esm.json",
|
||||
"build:cjs": "tsc --project tsconfig.cjs.json",
|
||||
"build:json": "node ./scripts/create_dist_package_json.js",
|
||||
"test:unit": "mocha -r ts-node/register",
|
||||
"test:integration": "mocha -r ts-node/register -r integrationTests/mochaRootHooks.ts",
|
||||
"tests:unit": "mocha -R progress -r ts-node/register ./tests/**/*.spec.ts",
|
||||
"tests:unit:watch": "mocha -w --watch-files . -R progress -r ts-node/register ./tests/**/*.spec.ts",
|
||||
"tests:integration": "mocha -R progress -r ts-node/register -r integrationTests/mochaRootHooks.ts ./integrationTests/**/*.spec.ts",
|
||||
"tests": "mocha -R progress -r ts-node/register ./tests/**/*.spec.ts && mocha -R progress -r ts-node/register -r integrationTests/mochaRootHooks.ts ./integrationTests/**/*.spec.ts",
|
||||
"test-coverage": "nyc npm run tests:unit",
|
||||
|
||||
@@ -111,6 +111,33 @@ export class OnspringClient {
|
||||
return apiResponse.asGetPagedAppsResponseType();
|
||||
}
|
||||
|
||||
/**
|
||||
* @method getAllApps - Gets an iterable that can be used to page through all.
|
||||
* @param {PagingRequest} pagingRequest - The paging request that will be used to get the apps.
|
||||
* @returns
|
||||
**/
|
||||
public async *getAllApps(
|
||||
pagingRequest: PagingRequest = new PagingRequest(1, 50)
|
||||
): AsyncGenerator<ApiResponse<GetPagedAppsResponse>, void, unknown> {
|
||||
// TODO: We need to actually honor the page request
|
||||
// the caller gave us. We will use the page number
|
||||
// as our starting page number and we will use the
|
||||
// page size as our page size
|
||||
for await (const page of this.getAllPagesAsync<GetPagedAppsResponse>(
|
||||
async (pr) => await this.getApps(pr)
|
||||
)) {
|
||||
yield page;
|
||||
}
|
||||
}
|
||||
|
||||
private async *getAllPagesAsync<T>(
|
||||
callback: (pagingRequest: PagingRequest) => Promise<ApiResponse<T>>
|
||||
): AsyncGenerator<ApiResponse<T>, void, unknown> {
|
||||
const pagingRequest = new PagingRequest(1, 50);
|
||||
const initialResponse = await callback(pagingRequest);
|
||||
yield initialResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* @method getAppById - Gets an app by its id.
|
||||
* @param {number} appId - The id of the app to get.
|
||||
|
||||
@@ -232,6 +232,85 @@ describe('OnspringClient', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAllApps', function () {
|
||||
it('should be define', function () {
|
||||
expect(new OnspringClient(baseUrl, apiKey).getAllApps).to.not.be
|
||||
.undefined;
|
||||
});
|
||||
|
||||
it('should be a function', function () {
|
||||
expect(new OnspringClient(baseUrl, apiKey).getAllApps).to.be.a(
|
||||
'function'
|
||||
);
|
||||
});
|
||||
|
||||
it('should have 0 parameter', function () {
|
||||
expect(new OnspringClient(baseUrl, apiKey).getAllApps).to.have.lengthOf(
|
||||
0
|
||||
);
|
||||
});
|
||||
|
||||
it('should return a async generator', function () {
|
||||
const result = new OnspringClient(baseUrl, apiKey).getAllApps();
|
||||
|
||||
expect(result).to.have.property(Symbol.asyncIterator);
|
||||
expect(result[Symbol.asyncIterator]).to.be.a('function');
|
||||
|
||||
expect(result).to.have.property('next').that.is.a('function');
|
||||
expect(result).to.have.property('return').that.is.a('function');
|
||||
expect(result).to.have.property('throw').that.is.a('function');
|
||||
});
|
||||
|
||||
it('should return a promise that resolves to an async iterable of paged response of apps when request is successful', async function () {
|
||||
const client = new OnspringClient(baseUrl, apiKey);
|
||||
|
||||
const mockAxiosClient = axios.create({
|
||||
baseURL: baseUrl,
|
||||
headers: {
|
||||
'x-apikey': apiKey,
|
||||
'x-api-version': '2',
|
||||
},
|
||||
});
|
||||
|
||||
sinon.stub(mockAxiosClient, 'get').returns(
|
||||
Promise.resolve({
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
data: {
|
||||
pageNumber: 1,
|
||||
pageSize: 2,
|
||||
totalPages: 1,
|
||||
totalRecords: 2,
|
||||
items: [
|
||||
{
|
||||
href: 'https://api.onspring.dev/Apps/id/1',
|
||||
id: '1',
|
||||
name: 'Test App 1',
|
||||
},
|
||||
{
|
||||
href: 'https://api.onspring.dev/Apps/id/2',
|
||||
id: '2',
|
||||
name: 'Test App 2',
|
||||
},
|
||||
],
|
||||
},
|
||||
headers: {},
|
||||
config: {} as InternalAxiosRequestConfig,
|
||||
} as AxiosResponse)
|
||||
);
|
||||
|
||||
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||
|
||||
const collectedPages: Array<ApiResponse<GetPagedAppsResponse>> = [];
|
||||
|
||||
for await (const page of client.getAllApps()) {
|
||||
collectedPages.push(page);
|
||||
}
|
||||
|
||||
expect(collectedPages.length).to.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getApps', function () {
|
||||
it('should be defined', function () {
|
||||
expect(new OnspringClient(baseUrl, apiKey).getApps).to.not.be.undefined;
|
||||
|
||||
Reference in New Issue
Block a user