From e90012ac5907018f03e9be1110de335661671a0d Mon Sep 17 00:00:00 2001 From: Adam Mcgrath Date: Fri, 8 Sep 2023 15:05:46 +0100 Subject: [PATCH 1/2] support optional responses --- playground/handlers.ts | 18 +++++++++++------- .../__generated/managers/guardian-manager.ts | 6 ++++-- .../__generated/managers/jobs-manager.ts | 6 ++++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/playground/handlers.ts b/playground/handlers.ts index ec8699453..3b6bb33c1 100644 --- a/playground/handlers.ts +++ b/playground/handlers.ts @@ -324,14 +324,16 @@ export async function clients() { const mgmntClient = new ManagementClient(program.opts()); const { data: newClient } = await mgmntClient.clients.create({ name: uuid() }); console.log('Create a client: ' + newClient.name); - const { data: client } = await mgmntClient.clients.get({ id: newClient.client_id as string }); + const { data: client } = await mgmntClient.clients.get({ + client_id: newClient.client_id as string, + }); console.log('Get the client: ' + client.name); const { data: updatedClient } = await mgmntClient.clients.update( - { id: client.client_id as string }, + { client_id: client.client_id as string }, { name: uuid() } ); console.log('Updated the client: ' + updatedClient.name); - await mgmntClient.clients.delete({ id: newClient.client_id as string }); + await mgmntClient.clients.delete({ client_id: newClient.client_id as string }); console.log('Removed the client: ' + updatedClient.name); } @@ -524,8 +526,8 @@ export async function guardian() { const { data: smsTemplates } = await mgmntClient.guardian.getSmsFactorTemplates(); - console.log(`Get SMS enrollement message: ${smsTemplates.enrollment_message}`); - console.log(`Get SMS verification message: ${smsTemplates.verification_message}`); + console.log(`Get SMS enrollement message: ${smsTemplates?.enrollment_message}`); + console.log(`Get SMS verification message: ${smsTemplates?.verification_message}`); const { data: updateSmsTemplates } = await mgmntClient.guardian.setSmsFactorTemplates({ enrollment_message: 'This is the encrollment message ' + uuid(), @@ -794,7 +796,9 @@ export async function keys() { const { data: newClient } = await mgmntClient.clients.create({ name: uuid() }); console.log('Create a client: ' + newClient.name); - const { data: client } = await mgmntClient.clients.get({ id: newClient.client_id as string }); + const { data: client } = await mgmntClient.clients.get({ + client_id: newClient.client_id as string, + }); const cert = client.signing_keys![0].cert; const { data: keys } = await mgmntClient.keys.getAll(); @@ -803,7 +807,7 @@ export async function keys() { const { data: key } = await mgmntClient.keys.get({ kid }); console.log('Got key:', key.kid); - await mgmntClient.clients.delete({ id: newClient.client_id as string }); + await mgmntClient.clients.delete({ client_id: newClient.client_id as string }); console.log('Removed the client: ' + newClient.name); } diff --git a/src/management/__generated/managers/guardian-manager.ts b/src/management/__generated/managers/guardian-manager.ts index 50cdce4be..4edf301a3 100644 --- a/src/management/__generated/managers/guardian-manager.ts +++ b/src/management/__generated/managers/guardian-manager.ts @@ -135,7 +135,7 @@ export class GuardianManager extends BaseAPI { */ async getSmsFactorTemplates( initOverrides?: InitOverride - ): Promise> { + ): Promise> { const response = await this.request( { path: `/guardian/factors/sms/templates`, @@ -144,7 +144,9 @@ export class GuardianManager extends BaseAPI { initOverrides ); - return runtime.JSONApiResponse.fromResponse(response); + return response.status === 204 + ? runtime.VoidApiResponse.fromResponse(response) + : runtime.JSONApiResponse.fromResponse(response); } /** diff --git a/src/management/__generated/managers/jobs-manager.ts b/src/management/__generated/managers/jobs-manager.ts index f1dc6a839..d2648aa5c 100644 --- a/src/management/__generated/managers/jobs-manager.ts +++ b/src/management/__generated/managers/jobs-manager.ts @@ -25,7 +25,7 @@ export class JobsManager extends BaseAPI { async getErrors( requestParameters: GetErrorsRequest, initOverrides?: InitOverride - ): Promise> { + ): Promise> { runtime.validateRequiredRequestParams(requestParameters, ['id']); const response = await this.request( @@ -36,7 +36,9 @@ export class JobsManager extends BaseAPI { initOverrides ); - return runtime.JSONApiResponse.fromResponse(response); + return response.status === 204 + ? runtime.VoidApiResponse.fromResponse(response) + : runtime.JSONApiResponse.fromResponse(response); } /** From 17b73d523ceb2c53168402712d04bf7ed1bf56b5 Mon Sep 17 00:00:00 2001 From: Adam Mcgrath Date: Fri, 8 Sep 2023 15:13:41 +0100 Subject: [PATCH 2/2] Add tests --- test/management/guardian.test.ts | 6 ++++++ test/management/jobs.test.ts | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/test/management/guardian.test.ts b/test/management/guardian.test.ts index b45b66176..46f464cf1 100644 --- a/test/management/guardian.test.ts +++ b/test/management/guardian.test.ts @@ -216,6 +216,12 @@ describe('GuardianManager', () => { await expect(guardian.getSmsFactorTemplates()).resolves.toHaveProperty('data', data); }); + + it('should not fail when no response returned', async () => { + nock(API_URL).get('/guardian/factors/sms/templates').reply(204); + + await expect(guardian.getSmsFactorTemplates()).resolves.not.toThrow(); + }); }); describe('#getFactors', () => { diff --git a/test/management/jobs.test.ts b/test/management/jobs.test.ts index 825f94ea8..69da7e272 100644 --- a/test/management/jobs.test.ts +++ b/test/management/jobs.test.ts @@ -166,6 +166,14 @@ describe('JobsManager', () => { done(); }); }); + + it('should not fail when no response returned', async () => { + nock.cleanAll(); + + nock(API_URL).get(`/jobs/${id}/errors`).reply(204); + + await expect(jobs.getErrors({ id: id })).resolves.not.toThrow(); + }); }); const usersFilePath = path.join(__dirname, '../data/users.json');