|
1 | 1 | import type { AnyContractRouter, HTTPPath } from '@orpc/contract' |
| 2 | +import type { StandardBody, StandardHeaders, StandardResponse } from '@orpc/standard-server' |
| 3 | +import type { NodeHttpResponse } from '@orpc/standard-server-node' |
| 4 | +import type { FastifyReply } from 'fastify/types/reply' |
| 5 | +import type { SendStandardResponseOptions } from '../../standard-server-aws-lambda/src' |
| 6 | +import { Readable } from 'node:stream' |
2 | 7 | import { toHttpPath } from '@orpc/client/standard' |
3 | 8 | import { ContractProcedure, isContractProcedure } from '@orpc/contract' |
4 | 9 | import { standardizeHTTPPath } from '@orpc/openapi-client/standard' |
5 | 10 | import { toArray } from '@orpc/shared' |
| 11 | +import { toNodeHttpBody } from '@orpc/standard-server-node' |
6 | 12 |
|
7 | 13 | export function toNestPattern(path: HTTPPath): string { |
8 | 14 | return standardizeHTTPPath(path) |
@@ -54,3 +60,70 @@ export function populateContractRouterPaths<T extends AnyContractRouter>(router: |
54 | 60 |
|
55 | 61 | return populated as any |
56 | 62 | } |
| 63 | + |
| 64 | +export function setStandardFastifyResponse( |
| 65 | + reply: FastifyReply, |
| 66 | + standardResponse: StandardResponse, |
| 67 | + options: SendStandardResponseOptions = { shouldStringifyBody: false }, |
| 68 | +) { |
| 69 | + if (options.shouldStringifyBody === undefined) { |
| 70 | + options.shouldStringifyBody = false |
| 71 | + } |
| 72 | + |
| 73 | + return new Promise((resolve, reject) => { |
| 74 | + reply.raw.once('error', reject) |
| 75 | + reply.raw.once('close', resolve) |
| 76 | + |
| 77 | + const resHeaders: StandardHeaders = { ...standardResponse.headers } |
| 78 | + |
| 79 | + const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options) |
| 80 | + |
| 81 | + reply.code(standardResponse.status) |
| 82 | + reply.headers(resHeaders) |
| 83 | + return resolve(resBody) |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +export function setStandardNodeResponse( |
| 88 | + res: NodeHttpResponse, |
| 89 | + standardResponse: StandardResponse, |
| 90 | + options: SendStandardResponseOptions = { shouldStringifyBody: false }, |
| 91 | +): Promise<void | StandardBody | undefined> { |
| 92 | + if (options.shouldStringifyBody === undefined) { |
| 93 | + options.shouldStringifyBody = false |
| 94 | + } |
| 95 | + |
| 96 | + return new Promise((resolve, reject) => { |
| 97 | + res.once('error', reject) |
| 98 | + res.once('close', resolve) |
| 99 | + |
| 100 | + const resHeaders: StandardHeaders = { ...standardResponse.headers } |
| 101 | + |
| 102 | + const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options) |
| 103 | + |
| 104 | + res.statusCode = standardResponse.status |
| 105 | + for (const [key, value] of Object.entries(resHeaders)) { |
| 106 | + if (value !== undefined) { |
| 107 | + res.setHeader(key, value) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (resBody === undefined) { |
| 112 | + return resolve(undefined) |
| 113 | + } |
| 114 | + else if (resBody instanceof Readable) { |
| 115 | + res.once('close', () => { |
| 116 | + if (!resBody.closed) { |
| 117 | + resBody.destroy(res.errored ?? undefined) |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + resBody.once('error', error => res.destroy(error)) |
| 122 | + |
| 123 | + resBody.pipe(res) |
| 124 | + } |
| 125 | + else { |
| 126 | + return resolve(resBody) |
| 127 | + } |
| 128 | + }) |
| 129 | +} |
0 commit comments