Skip to content

Commit

Permalink
style: format fixed!
Browse files Browse the repository at this point in the history
  • Loading branch information
unnoq committed Nov 18, 2024
1 parent 90282e4 commit 76179b3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
39 changes: 21 additions & 18 deletions packages/server/src/adapters/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,22 +398,21 @@ describe('file upload', () => {
})
})


describe("accept header", () => {
describe('accept header', () => {
const router = os.router({
ping: os.handler(async () => 'pong'),
})
const handler = createFetchHandler({
router,
})

it("application/json", async () => {
it('application/json', async () => {
const response = await handler({
prefix: '/orpc',
request: new Request('http://localhost/orpc/ping', {
method: 'POST',
headers: {
'Accept': 'application/json',
Accept: 'application/json',
},
}),
})
Expand All @@ -423,47 +422,51 @@ describe("accept header", () => {
expect(await response.json()).toEqual('pong')
})

it("multipart/form-data", async () => {
it('multipart/form-data', async () => {
const response = await handler({
prefix: '/orpc',
request: new Request('http://localhost/orpc/ping', {
method: 'POST',
headers: {
'Accept': 'multipart/form-data',
Accept: 'multipart/form-data',
},
}),
})

expect(response.headers.get('Content-Type')).toContain('multipart/form-data')
expect(response.headers.get('Content-Type')).toContain(
'multipart/form-data',
)

const form = await response.formData()
expect(form.get('')).toEqual('pong')
})

it("application/x-www-form-urlencoded", async () => {
it('application/x-www-form-urlencoded', async () => {
const response = await handler({
prefix: '/orpc',
request: new Request('http://localhost/orpc/ping', {
method: 'POST',
headers: {
'Accept': 'application/x-www-form-urlencoded',
Accept: 'application/x-www-form-urlencoded',
},
}),
})

expect(response.headers.get('Content-Type')).toEqual('application/x-www-form-urlencoded')
expect(response.headers.get('Content-Type')).toEqual(
'application/x-www-form-urlencoded',
)

const params = new URLSearchParams(await response.text())
expect(params.get('')).toEqual('pong')
})

it("*/*", async () => {
it('*/*', async () => {
const response = await handler({
prefix: '/orpc',
request: new Request('http://localhost/orpc/ping', {
method: 'POST',
headers: {
'Accept': '*/*',
Accept: '*/*',
},
}),
})
Expand All @@ -472,22 +475,22 @@ describe("accept header", () => {
expect(await response.json()).toEqual('pong')
})

it("invalid", async () => {
it('invalid', async () => {
const response = await handler({
prefix: '/orpc',
request: new Request('http://localhost/orpc/ping', {
method: 'POST',
headers: {
'Accept': 'invalid',
Accept: 'invalid',
},
}),
})

expect(response.headers.get('Content-Type')).toEqual('application/json')
expect(await response.json()).toEqual({
"code": "NOT_ACCEPTABLE",
"message": "Unsupported content-type: invalid",
"status": 406,
code: 'NOT_ACCEPTABLE',
message: 'Unsupported content-type: invalid',
status: 406,
})
})
})
})
19 changes: 10 additions & 9 deletions packages/server/src/adapters/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ export function createFetchHandler<TRouter extends Router<any>>(
const deserializer = isORPCTransformer
? new ORPCDeserializer()
: new OpenAPIDeserializer({
schema: procedure.zz$p.contract.zz$cp.InputSchema,
})
schema: procedure.zz$p.contract.zz$cp.InputSchema,
})

const input_ = await (async () => {
try {
Expand Down Expand Up @@ -193,7 +193,9 @@ export function createFetchHandler<TRouter extends Router<any>>(
const error = toORPCError(e)

// fallback to OpenAPI serializer (without accept) when expected serializer has failed
const { body, headers } = new OpenAPISerializer().serialize(error.toJSON())
const { body, headers } = new OpenAPISerializer().serialize(
error.toJSON(),
)

return new Response(body, {
status: error.status,
Expand Down Expand Up @@ -232,13 +234,12 @@ export interface FetchHandler<TRouter extends Router<any>> {
(options: FetchHandlerOptions<TRouter>): Promise<Response>
}


function toORPCError(e: unknown): ORPCError<any, any> {
return e instanceof ORPCError
? e
: new ORPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Internal server error',
cause: e,
})
}
code: 'INTERNAL_SERVER_ERROR',
message: 'Internal server error',
cause: e,
})
}
8 changes: 8 additions & 0 deletions packages/shared/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ export function get(
let preSegment: string | number = 'root'

for (const segment of segments) {
if (typeof currentRef !== 'object' || currentRef === null) {
return undefined
}

currentRef = currentRef[preSegment]
preSegment = segment
}

if (typeof currentRef !== 'object' || currentRef === null) {
return undefined
}

return currentRef[preSegment]
}

Expand Down

0 comments on commit 76179b3

Please sign in to comment.