Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sendPostBinary #63

Merged
merged 3 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions src/http/httpClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
sendGet,
sendPatch,
sendPost,
sendPostBinary,
sendPut,
sendPutBinary,
} from './httpClient'
Expand Down Expand Up @@ -538,6 +539,156 @@ describe('httpClient', () => {
})
})

describe('POST binary', () => {
it('validates response structure with provided schema, throws an error', async () => {
const schema = z.object({
id: z.string(),
})

client
.intercept({
path: '/products/1',
method: 'POST',
})
.reply(200, mockProduct1, { headers: JSON_HEADERS })

await expect(
sendPostBinary(client, '/products/1', Buffer.from(JSON.stringify({})), {
responseSchema: schema,
validateResponse: true,
}),
).rejects.toThrow(/Expected string, received number/)
})

it('validates response structure with provided schema, passes validation', async () => {
const schema = z.object({
category: z.string(),
description: z.string(),
id: z.number(),
image: z.string(),
price: z.number(),
rating: z.object({
count: z.number(),
rate: z.number(),
}),
title: z.string(),
})

client
.intercept({
path: '/products/1',
method: 'POST',
})
.reply(200, mockProduct1, { headers: JSON_HEADERS })

const result = await sendPostBinary(client, '/products/1', Buffer.from(JSON.stringify({})), {
responseSchema: schema,
validateResponse: true,
reqContext,
})

expect(result.result.body).toEqual(mockProduct1)
})

it('validates response structure with provided schema, skips validation', async () => {
const schema = z.object({
id: z.string(),
})

client
.intercept({
path: '/products/1',
method: 'POST',
})
.reply(200, mockProduct1, { headers: JSON_HEADERS })

const result = await sendPostBinary(client, '/products/1', Buffer.from(JSON.stringify({})), {
responseSchema: schema,
validateResponse: false,
})

expect(result.result.body).toEqual(mockProduct1)
})

it('POST without queryParams', async () => {
client
.intercept({
path: '/products',
method: 'POST',
})
.reply(200, { id: 21 }, { headers: JSON_HEADERS })

const result = await sendPostBinary(
client,
'/products',
Buffer.from(JSON.stringify(mockProduct1)),
)

expect(result.result.body).toEqual({ id: 21 })
})

it('POST with queryParams', async () => {
const query = {
limit: 3,
}

client
.intercept({
path: '/products',
method: 'POST',
query,
})
.reply(200, { id: 21 }, { headers: JSON_HEADERS })

const result = await sendPostBinary(
client,
'/products',
Buffer.from(JSON.stringify(mockProduct1)),
{
query,
},
)

expect(result.result.body).toEqual({ id: 21 })
})

it('POST that returns 400 throws an error', async () => {
client
.intercept({
path: '/products',
method: 'POST',
})
.reply(400, { errorCode: 'err' }, { headers: JSON_HEADERS })

await expect(
sendPostBinary(client, '/products', Buffer.from(JSON.stringify(mockProduct1))),
).rejects.toThrow('Response status code 400')
})

it('Throws an error on internal error', async () => {
expect.assertions(1)
const query = {
limit: 3,
}

client
.intercept({
path: '/products',
method: 'POST',
query,
})
.replyWithError(new Error('connection error'))

await expect(
sendPostBinary(client, '/products', Buffer.from(JSON.stringify({})), {
query,
}),
).rejects.toMatchObject({
message: 'connection error',
})
})
})

describe('PUT', () => {
it('PUT without queryParams', async () => {
client
Expand Down
35 changes: 34 additions & 1 deletion src/http/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,39 @@ export async function sendPost<T>(
)
}

export async function sendPostBinary<T>(
client: Client,
path: string,
body: Buffer | Uint8Array | Readable | FormData | null,
options: Partial<RequestOptions<T>> = {},
): Promise<DefiniteEither<RequestResult<unknown>, RequestResult<T>>> {
const result = await sendWithRetry<T>(
client,
{
...DEFAULT_OPTIONS,
path: path,
method: 'POST',
body,
query: options.query,
headers: copyWithoutUndefined({
'x-request-id': options.reqContext?.reqId,
...options.headers,
}),
reset: options.disableKeepAlive ?? false,
bodyTimeout: options.timeout,
throwOnError: false,
},
resolveRetryConfig(options),
)

return resolveResult(
result,
options.throwOnError ?? DEFAULT_OPTIONS.throwOnError,
options.validateResponse ?? DEFAULT_OPTIONS.validateResponse,
options.responseSchema,
)
}

export async function sendPut<T>(
client: Client,
path: string,
Expand Down Expand Up @@ -183,7 +216,7 @@ export async function sendPut<T>(
export async function sendPutBinary<T>(
client: Client,
path: string,
body: Buffer | Uint8Array | Readable | null | FormData,
body: Buffer | Uint8Array | Readable | FormData | null,
options: Partial<RequestOptions<T>> = {},
): Promise<DefiniteEither<RequestResult<unknown>, RequestResult<T>>> {
const result = await sendWithRetry<T>(
Expand Down