Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #15 from shinzui/body-encoding
Browse files Browse the repository at this point in the history
Support schemas with body encoding
  • Loading branch information
drwpow authored Apr 11, 2023
2 parents aea98e0 + 4a8ac40 commit fce1546
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,28 @@ describe('post()', () => {
expect(error).toBe(undefined);
});

it('supports sepecifying utf-8 encoding', async () => {
const mockData = { message: 'My reply' };
const client = createClient<paths>();
fetchMocker.mockResponseOnce(() => ({ status: 201, body: JSON.stringify(mockData) }));
const { data, error, response } = await client.post('/create-reply', {
body: {
message: 'My reply',
replied_at: new Date('2023-03-31T12:00:00Z').getTime(),
},
});

// assert correct URL was called
expect(fetchMocker.mock.calls[0][0]).toBe('/create-reply');

// assert correct data was returned
expect(data).toEqual(mockData);
expect(response.status).toBe(201);

// assert error is empty
expect(error).toBe(undefined);
});

it('supports optional requestBody', async () => {
const mockData = { status: 'success' };
const client = createClient<paths>();
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Unwrap<T> = T extends {
content: { 'application/json': any };
}
? T['content']['application/json']
: T extends { content: { 'application/json;charset=utf-8': any } }
? T['content']['application/json;charset=utf-8']
: T extends { content: { '*/*': any } }
? T['content']['*/*']
: T;
Expand Down
24 changes: 24 additions & 0 deletions test/v1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export interface paths {
};
};
};
"/create-reply": {
post: {
requestBody: components["requestBodies"]["CreateReply"];
responses: {
201: components["responses"]["CreateReply"];
500: components["responses"]["Error"];
};
};
};
"/self": {
get: {
responses: {
Expand Down Expand Up @@ -171,6 +180,13 @@ export interface components {
};
};
};
CreateReply: {
content: {
"application/json;charset=utf-8": {
message: string;
};
};
};
Error: {
content: {
"application/json": {
Expand Down Expand Up @@ -220,6 +236,14 @@ export interface components {
};
};
};
CreateReply: {
content: {
"application/json;charset=utf-8": {
message: string;
replied_at: number;
};
};
};
};
headers: never;
pathItems: never;
Expand Down
34 changes: 33 additions & 1 deletion test/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ paths:
$ref: '#/components/responses/PostDelete'
500:
$ref: '#/components/responses/Error'

/create-reply:
post:
requestBody:
$ref: '#/components/requestBodies/CreateReply'
responses:
201:
$ref: '#/components/responses/CreateReply'
500:
$ref: '#/components/responses/Error'
/self:
get:
responses:
Expand Down Expand Up @@ -184,6 +192,20 @@ components:
properties:
description:
type: string
CreateReply:
required: true
content:
'application/json;charset=utf-8':
schema:
type: object
properties:
message:
type: string
replied_at:
type: number
required:
- message
- replied_at
responses:
CreatePost:
content:
Expand All @@ -205,6 +227,16 @@ components:
type: string
required:
- status
CreateReply:
content:
'application/json;charset=utf-8':
schema:
type: object
properties:
message:
type: string
required:
- message
Error:
content:
application/json:
Expand Down

0 comments on commit fce1546

Please sign in to comment.