Skip to content

Commit

Permalink
feat: return all content in data when no response property is defined (
Browse files Browse the repository at this point in the history
…#145)

Allow the response type not to be an object since it can also return any
type when no `data` is found in the response
  • Loading branch information
Cysword authored Dec 18, 2023
1 parent f67cb4e commit 5b6e274
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/model/client/AbstractClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {TestGetTextEndpoint} from '@Test/endpoints/TestGetTextEndpoint';
import {TestGetInlineContentEndpoint} from '@Test/endpoints/TestGetInlineContentEndpoint';
import {TestGetAttachmentEndpoint} from '@Test/endpoints/TestGetAttachmentEndpoint';
import {TestGet200Endpoint} from '@Test/endpoints/TestGet200Endpoint';
import {TestGet200NoResponseProperty} from '@Test/endpoints/TestGet200NoResponseProperty';
import {TestDeleteEndpoint} from '@Test/endpoints/TestDeleteEndpoint';
import {UserException} from '@/model/exception/UserException';
import {ApiException} from '@/model/exception/ApiException';
Expand Down Expand Up @@ -400,4 +401,14 @@ describe('AbstractClient', () => {
expect(response).toHaveProperty('shipments', []);
expect(response).toHaveProperty('results', 0);
});

it('returns all contents from data property when no property is defined', async () => {
expect.assertions(2);

const sdk = createPublicSdk(new FetchClient(), [new TestGet200NoResponseProperty()]);
const response = await sdk.get200NoResponseProperty();

expect(response).toHaveProperty('token', 'test');
expect(response).toHaveProperty('credentials', {username: 'test', password: 'test'});
});
});
5 changes: 5 additions & 0 deletions src/model/client/AbstractClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export abstract class AbstractClient {

if (isOfType<ResponseWrapper<EndpointResponseBody<E>>>(response, 'data')) {
const property = endpoint.getResponseProperty() as EndpointResponseProperty<E>;

if (!property) {
return response.data;
}

let wrappedResponse: EndpointResponse<E> = response.data[property];

// If the response is paginated, wrap it.
Expand Down
3 changes: 2 additions & 1 deletion src/model/endpoint/AbstractEndpoint.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {type RequestHeaders} from '@/types/request.types';
import {type NoInfer} from '@/types/global.types';
import { type OneOrMore } from '@myparcel/ts-utils';

type Pagination = {
page?: number;
Expand All @@ -15,7 +16,7 @@ export interface EndpointDefinition {
headers?: RequestHeaders;
parameters?: NoInfer<Record<string, string | number | boolean>>;
path?: Record<string, string | number>;
response?: NoInfer<unknown[]> | PaginatedResponse<NoInfer<unknown[]>>;
response?: NoInfer<OneOrMore<unknown>> | PaginatedResponse<NoInfer<unknown[]>>;
}

export type CreateDefinition<D extends EndpointDefinition> = D;
6 changes: 6 additions & 0 deletions test/endpoints/TestGet200NoResponseProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {AbstractPublicEndpoint} from '@/model';

export class TestGet200NoResponseProperty extends AbstractPublicEndpoint {
public readonly name = 'get200NoResponseProperty';
public readonly path = 'get200NoResponseProperty';
}
21 changes: 21 additions & 0 deletions test/fetch/examples/get-200-no-response-property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// noinspection JSUnusedGlobalSymbols

import {defineMockResponse} from '@Test/fetch/defineMockResponse';

export default defineMockResponse({
match: (path: string, init?: RequestInit) => init?.method === 'GET' && path.startsWith('/get200NoResponseProperty'),

response: () => ({
headers: {'Content-Type': 'application/json'},
status: 200,
body: {
data: {
token: 'test',
credentials: {
username: 'test',
password: 'test',
},
},
},
}),
});

0 comments on commit 5b6e274

Please sign in to comment.