-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
83 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
import { z } from 'zod'; | ||
|
||
import { OpenApiProcedureRecord, OpenApiRouter } from '../../types'; | ||
import { forEachOpenApiProcedure, getInputOutputParsers } from '../../utils/procedure'; | ||
import { OpenApiProcedure } from '../../types'; | ||
import { getInputOutputParsers } from '../../utils/procedure'; | ||
import { instanceofZodType, instanceofZodTypeLikeVoid } from '../../utils/zod'; | ||
|
||
export const monkeyPatchVoidInputs = (appRouter: OpenApiRouter) => { | ||
const { queries, mutations } = appRouter._def; | ||
const zObject = z.object({}); | ||
type MonkeyPatchedOpenApiProcedure = OpenApiProcedure & { __MONKEY_PATCHED__?: boolean }; | ||
|
||
const voidInputPatcher = (procedure: OpenApiProcedureRecord[string]) => { | ||
const { inputParser } = getInputOutputParsers(procedure); | ||
if (instanceofZodType(inputParser) && instanceofZodTypeLikeVoid(inputParser)) { | ||
export const monkeyPatchProcedure = (procedure: MonkeyPatchedOpenApiProcedure) => { | ||
if (procedure.__MONKEY_PATCHED__) return; | ||
procedure.__MONKEY_PATCHED__ = true; | ||
|
||
const { inputParser } = getInputOutputParsers(procedure); | ||
if (instanceofZodType(inputParser)) { | ||
if (instanceofZodTypeLikeVoid(inputParser)) { | ||
const zObject = z.object({}); | ||
(procedure as any).parseInputFn = zObject.parseAsync.bind(zObject); | ||
} | ||
}; | ||
|
||
forEachOpenApiProcedure(queries, ({ procedure }) => voidInputPatcher(procedure)); | ||
forEachOpenApiProcedure(mutations, ({ procedure }) => voidInputPatcher(procedure)); | ||
// TODO: add out of box support for number/boolean/date etc. (https://github.com/jlalmes/trpc-openapi/issues/44) | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters