diff --git a/src/index.test.ts b/src/index.test.ts index a6ff81a..41ca640 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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(); + 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(); diff --git a/src/index.ts b/src/index.ts index 864ff92..210ffba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,6 +22,8 @@ type Unwrap = 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; diff --git a/test/v1.d.ts b/test/v1.d.ts index af06436..d6eb4ae 100644 --- a/test/v1.d.ts +++ b/test/v1.d.ts @@ -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: { @@ -171,6 +180,13 @@ export interface components { }; }; }; + CreateReply: { + content: { + "application/json;charset=utf-8": { + message: string; + }; + }; + }; Error: { content: { "application/json": { @@ -220,6 +236,14 @@ export interface components { }; }; }; + CreateReply: { + content: { + "application/json;charset=utf-8": { + message: string; + replied_at: number; + }; + }; + }; }; headers: never; pathItems: never; diff --git a/test/v1.yaml b/test/v1.yaml index 17c9694..25fc958 100644 --- a/test/v1.yaml +++ b/test/v1.yaml @@ -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: @@ -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: @@ -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: