@orpc/openapi 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,27 +1,19 @@
1
1
  import {
2
- eachContractProcedureLeaf,
2
+ OpenAPIPayloadCodec,
3
+ forEachContractProcedure,
3
4
  standardizeHTTPPath
4
- } from "./chunk-CMRY2Z4J.js";
5
+ } from "./chunk-UPDKQRQG.js";
5
6
 
6
7
  // src/generator.ts
7
8
  import { isContractProcedure } from "@orpc/contract";
8
9
  import { unlazy } from "@orpc/server";
9
10
  import { findDeepMatches, isPlainObject, omit } from "@orpc/shared";
10
- import { preSerialize } from "@orpc/transformer";
11
11
  import {
12
12
  OpenApiBuilder
13
13
  } from "openapi3-ts/oas31";
14
14
 
15
- // src/zod-to-json-schema.ts
16
- import {
17
- getCustomJSONSchema,
18
- getCustomZodFileMimeType,
19
- getCustomZodType
20
- } from "@orpc/zod";
21
- import escapeStringRegexp from "escape-string-regexp";
22
- import {
23
- Format
24
- } from "json-schema-typed/draft-2020-12";
15
+ // ../zod/src/coercer.ts
16
+ import { guard } from "@orpc/shared";
25
17
 
26
18
  // ../../node_modules/.pnpm/zod@3.24.1/node_modules/zod/lib/index.mjs
27
19
  var util;
@@ -3913,7 +3905,37 @@ var nullableType = ZodNullable.create;
3913
3905
  var preprocessType = ZodEffects.createWithPreprocess;
3914
3906
  var pipelineType = ZodPipeline.create;
3915
3907
 
3908
+ // ../zod/src/schemas.ts
3909
+ import wcmatch from "wildcard-match";
3910
+ var customZodTypeSymbol = Symbol("customZodTypeSymbol");
3911
+ var customZodFileMimeTypeSymbol = Symbol("customZodFileMimeTypeSymbol");
3912
+ var CUSTOM_JSON_SCHEMA_SYMBOL = Symbol("CUSTOM_JSON_SCHEMA");
3913
+ var CUSTOM_JSON_SCHEMA_INPUT_SYMBOL = Symbol("CUSTOM_JSON_SCHEMA_INPUT");
3914
+ var CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL = Symbol("CUSTOM_JSON_SCHEMA_OUTPUT");
3915
+ function getCustomZodType(def) {
3916
+ return customZodTypeSymbol in def ? def[customZodTypeSymbol] : void 0;
3917
+ }
3918
+ function getCustomZodFileMimeType(def) {
3919
+ return customZodFileMimeTypeSymbol in def ? def[customZodFileMimeTypeSymbol] : void 0;
3920
+ }
3921
+ function getCustomJSONSchema(def, options) {
3922
+ if (options?.mode === "input" && CUSTOM_JSON_SCHEMA_INPUT_SYMBOL in def) {
3923
+ return def[CUSTOM_JSON_SCHEMA_INPUT_SYMBOL];
3924
+ }
3925
+ if (options?.mode === "output" && CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL in def) {
3926
+ return def[CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL];
3927
+ }
3928
+ if (CUSTOM_JSON_SCHEMA_SYMBOL in def) {
3929
+ return def[CUSTOM_JSON_SCHEMA_SYMBOL];
3930
+ }
3931
+ return void 0;
3932
+ }
3933
+
3916
3934
  // src/zod-to-json-schema.ts
3935
+ import escapeStringRegexp from "escape-string-regexp";
3936
+ import {
3937
+ Format
3938
+ } from "json-schema-typed/draft-2020-12";
3917
3939
  var NON_LOGIC_KEYWORDS = [
3918
3940
  // Core Documentation Keywords
3919
3941
  "$anchor",
@@ -4359,6 +4381,7 @@ function extractJSONSchema(schema, check, matches = []) {
4359
4381
  async function generateOpenAPI(opts, options) {
4360
4382
  const throwOnMissingTagDefinition = options?.throwOnMissingTagDefinition ?? false;
4361
4383
  const ignoreUndefinedPathProcedures = options?.ignoreUndefinedPathProcedures ?? false;
4384
+ const payloadCodec = options?.payloadCodec ?? new OpenAPIPayloadCodec();
4362
4385
  const builder = new OpenApiBuilder({
4363
4386
  ...omit(opts, ["router"]),
4364
4387
  openapi: "3.1.0"
@@ -4369,7 +4392,7 @@ async function generateOpenAPI(opts, options) {
4369
4392
  router: opts.router
4370
4393
  }];
4371
4394
  for (const item of pending) {
4372
- const lazies = eachContractProcedureLeaf(item, ({ contract, path }) => {
4395
+ const lazies = forEachContractProcedure(item, ({ contract, path }) => {
4373
4396
  if (!isContractProcedure(contract)) {
4374
4397
  return;
4375
4398
  }
@@ -4565,14 +4588,14 @@ async function generateOpenAPI(opts, options) {
4565
4588
  });
4566
4589
  });
4567
4590
  for (const lazy of lazies) {
4568
- const { default: router } = await unlazy(lazy.lazy);
4591
+ const { default: router } = await unlazy(lazy.router);
4569
4592
  pending.push({
4570
4593
  path: lazy.path,
4571
4594
  router
4572
4595
  });
4573
4596
  }
4574
4597
  }
4575
- return preSerialize(builder.getSpec());
4598
+ return payloadCodec.serialize(builder.getSpec());
4576
4599
  }
4577
4600
  function isFileSchema(schema) {
4578
4601
  if (typeof schema !== "object" || schema === null)
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Serialize an object or array into a list of [key, value] pairs.
3
+ * The key will express by using bracket-notation.
4
+ *
5
+ * Notice: This way cannot express the empty object or array.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const payload = {
10
+ * name: 'John Doe',
11
+ * pets: ['dog', 'cat'],
12
+ * }
13
+ *
14
+ * const entities = serialize(payload)
15
+ *
16
+ * expect(entities).toEqual([
17
+ * ['name', 'John Doe'],
18
+ * ['name[pets][0]', 'dog'],
19
+ * ['name[pets][1]', 'cat'],
20
+ * ])
21
+ * ```
22
+ */
23
+ export declare function serialize(payload: unknown, parentKey?: string): [string, unknown][];
24
+ /**
25
+ * Deserialize a list of [key, value] pairs into an object or array.
26
+ * The key is expressed by using bracket-notation.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const entities = [
31
+ * ['name', 'John Doe'],
32
+ * ['name[pets][0]', 'dog'],
33
+ * ['name[pets][1]', 'cat'],
34
+ * ['name[dogs][]', 'hello'],
35
+ * ['name[dogs][]', 'kitty'],
36
+ * ]
37
+ *
38
+ * const payload = deserialize(entities)
39
+ *
40
+ * expect(payload).toEqual({
41
+ * name: 'John Doe',
42
+ * pets: { 0: 'dog', 1: 'cat' },
43
+ * dogs: ['hello', 'kitty'],
44
+ * })
45
+ * ```
46
+ */
47
+ export declare function deserialize(entities: readonly (readonly [string, unknown])[]): Record<string, unknown> | unknown[] | undefined;
48
+ /**
49
+ * Escape the `[`, `]`, and `\` chars in a path segment.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * expect(escapeSegment('name[pets')).toEqual('name\\[pets')
54
+ * ```
55
+ */
56
+ export declare function escapeSegment(segment: string): string;
57
+ /**
58
+ * Convert an array of path segments into a path string using bracket-notation.
59
+ *
60
+ * For the special char `[`, `]`, and `\` will be escaped by adding `\` at start.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * expect(stringifyPath(['name', 'pets', '0'])).toEqual('name[pets][0]')
65
+ * ```
66
+ */
67
+ export declare function stringifyPath(path: readonly [string, ...string[]]): string;
68
+ /**
69
+ * Convert a path string using bracket-notation into an array of path segments.
70
+ *
71
+ * For the special char `[`, `]`, and `\` you should escape by adding `\` at start.
72
+ * It only treats a pair `[${string}]` as a path segment.
73
+ * If missing or escape it will bypass and treat as normal string.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * expect(parsePath('name[pets][0]')).toEqual(['name', 'pets', '0'])
78
+ * expect(parsePath('name[pets][0')).toEqual(['name', 'pets', '[0'])
79
+ * expect(parsePath('name[pets[0]')).toEqual(['name', 'pets[0')
80
+ * expect(parsePath('name\\[pets][0]')).toEqual(['name[pets]', '0'])
81
+ * ```
82
+ */
83
+ export declare function parsePath(path: string): [string, ...string[]];
84
+ //# sourceMappingURL=bracket-notation.d.ts.map
@@ -1,4 +1,10 @@
1
- export * from './base-handler';
2
- export * from './server-handler';
3
- export * from './serverless-handler';
1
+ export * from './bracket-notation';
2
+ export * from './input-builder-full';
3
+ export * from './input-builder-simple';
4
+ export * from './openapi-handler';
5
+ export * from './openapi-handler-server';
6
+ export * from './openapi-handler-serverless';
7
+ export * from './openapi-payload-codec';
8
+ export * from './openapi-procedure-matcher';
9
+ export * from './schema-coercer';
4
10
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,11 @@
1
+ import type { Params } from 'hono/router';
2
+ export declare class InputBuilderFull {
3
+ build(params: Params, query: unknown, headers: unknown, body: unknown): {
4
+ params: Params;
5
+ query: unknown;
6
+ headers: unknown;
7
+ body: unknown;
8
+ };
9
+ }
10
+ export type PublicInputBuilderFull = Pick<InputBuilderFull, keyof InputBuilderFull>;
11
+ //# sourceMappingURL=input-builder-full.d.ts.map
@@ -0,0 +1,6 @@
1
+ import type { Params } from 'hono/router';
2
+ export declare class InputBuilderSimple {
3
+ build(params: Params, payload: unknown): unknown;
4
+ }
5
+ export type PublicInputBuilderSimple = Pick<InputBuilderSimple, keyof InputBuilderSimple>;
6
+ //# sourceMappingURL=input-builder-simple.d.ts.map
@@ -0,0 +1,7 @@
1
+ import type { Context, Router } from '@orpc/server';
2
+ import type { OpenAPIHandlerOptions } from './openapi-handler';
3
+ import { OpenAPIHandler } from './openapi-handler';
4
+ export declare class OpenAPIServerHandler<T extends Context> extends OpenAPIHandler<T> {
5
+ constructor(router: Router<T, any>, options?: NoInfer<OpenAPIHandlerOptions<T>>);
6
+ }
7
+ //# sourceMappingURL=openapi-handler-server.d.ts.map
@@ -0,0 +1,7 @@
1
+ import type { Context, Router } from '@orpc/server';
2
+ import type { OpenAPIHandlerOptions } from './openapi-handler';
3
+ import { OpenAPIHandler } from './openapi-handler';
4
+ export declare class OpenAPIServerlessHandler<T extends Context> extends OpenAPIHandler<T> {
5
+ constructor(router: Router<T, any>, options?: NoInfer<OpenAPIHandlerOptions<T>>);
6
+ }
7
+ //# sourceMappingURL=openapi-handler-serverless.d.ts.map
@@ -0,0 +1,28 @@
1
+ import type { ConditionalFetchHandler, FetchOptions } from '@orpc/server/fetch';
2
+ import type { PublicInputBuilderSimple } from './input-builder-simple';
3
+ import { type Context, type Router, type WithSignal } from '@orpc/server';
4
+ import { type Hooks } from '@orpc/shared';
5
+ import { type PublicInputBuilderFull } from './input-builder-full';
6
+ import { type PublicOpenAPIPayloadCodec } from './openapi-payload-codec';
7
+ import { type Hono, type PublicOpenAPIProcedureMatcher } from './openapi-procedure-matcher';
8
+ import { type SchemaCoercer } from './schema-coercer';
9
+ export type OpenAPIHandlerOptions<T extends Context> = Hooks<Request, Response, T, WithSignal> & {
10
+ procedureMatcher?: PublicOpenAPIProcedureMatcher;
11
+ payloadCodec?: PublicOpenAPIPayloadCodec;
12
+ inputBuilderSimple?: PublicInputBuilderSimple;
13
+ inputBuilderFull?: PublicInputBuilderFull;
14
+ schemaCoercers?: SchemaCoercer[];
15
+ };
16
+ export declare class OpenAPIHandler<T extends Context> implements ConditionalFetchHandler<T> {
17
+ private readonly options?;
18
+ private readonly procedureMatcher;
19
+ private readonly payloadCodec;
20
+ private readonly inputBuilderSimple;
21
+ private readonly inputBuilderFull;
22
+ private readonly compositeSchemaCoercer;
23
+ constructor(hono: Hono, router: Router<T, any>, options?: NoInfer<OpenAPIHandlerOptions<T>> | undefined);
24
+ condition(request: Request): boolean;
25
+ fetch(request: Request, ...[options]: [options: FetchOptions<T>] | (undefined extends T ? [] : never)): Promise<Response>;
26
+ private convertToORPCError;
27
+ }
28
+ //# sourceMappingURL=openapi-handler.d.ts.map
@@ -0,0 +1,13 @@
1
+ export declare class OpenAPIPayloadCodec {
2
+ encode(payload: unknown, accept?: string): {
3
+ body: FormData | Blob | string | undefined;
4
+ headers?: Headers;
5
+ };
6
+ private encodeAsJSON;
7
+ private encodeAsFormData;
8
+ private encodeAsURLSearchParams;
9
+ serialize(payload: unknown): unknown;
10
+ decode(re: Request | Response | Headers | URLSearchParams | FormData): Promise<unknown>;
11
+ }
12
+ export type PublicOpenAPIPayloadCodec = Pick<OpenAPIPayloadCodec, keyof OpenAPIPayloadCodec>;
13
+ //# sourceMappingURL=openapi-payload-codec.d.ts.map
@@ -0,0 +1,19 @@
1
+ import type { Router as BaseHono, Params } from 'hono/router';
2
+ import { type ANY_PROCEDURE, type ANY_ROUTER } from '@orpc/server';
3
+ export type Hono = BaseHono<[string, string[]]>;
4
+ export declare class OpenAPIProcedureMatcher {
5
+ private readonly hono;
6
+ private readonly router;
7
+ private pendingRouters;
8
+ constructor(hono: Hono, router: ANY_ROUTER);
9
+ match(method: string, pathname: string): Promise<{
10
+ path: string[];
11
+ procedure: ANY_PROCEDURE;
12
+ params: Params;
13
+ } | undefined>;
14
+ private add;
15
+ private handlePendingRouters;
16
+ private convertOpenAPIPathToRouterPath;
17
+ }
18
+ export type PublicOpenAPIProcedureMatcher = Pick<OpenAPIProcedureMatcher, keyof OpenAPIProcedureMatcher>;
19
+ //# sourceMappingURL=openapi-procedure-matcher.d.ts.map
@@ -0,0 +1,10 @@
1
+ import type { Schema } from '@orpc/contract';
2
+ export interface SchemaCoercer {
3
+ coerce: (schema: Schema, value: unknown) => unknown;
4
+ }
5
+ export declare class CompositeSchemaCoercer implements SchemaCoercer {
6
+ private readonly coercers;
7
+ constructor(coercers: SchemaCoercer[]);
8
+ coerce(schema: Schema, value: unknown): unknown;
9
+ }
10
+ //# sourceMappingURL=schema-coercer.d.ts.map
@@ -1,3 +1,4 @@
1
+ import type { PublicOpenAPIPayloadCodec } from './fetch';
1
2
  import { type ContractRouter } from '@orpc/contract';
2
3
  import { type ANY_ROUTER } from '@orpc/server';
3
4
  import { type OpenAPIObject } from 'openapi3-ts/oas31';
@@ -17,6 +18,7 @@ export interface GenerateOpenAPIOptions {
17
18
  * @default false
18
19
  */
19
20
  ignoreUndefinedPathProcedures?: boolean;
21
+ payloadCodec?: PublicOpenAPIPayloadCodec;
20
22
  }
21
23
  export declare function generateOpenAPI(opts: {
22
24
  router: ContractRouter | ANY_ROUTER;
@@ -9,9 +9,9 @@ export interface EachLeafCallbackOptions {
9
9
  path: string[];
10
10
  }
11
11
  export interface EachContractLeafResultItem {
12
- lazy: Lazy<ANY_PROCEDURE> | Lazy<Record<string, ANY_ROUTER>>;
12
+ router: Lazy<ANY_PROCEDURE> | Lazy<Record<string, ANY_ROUTER> | ANY_PROCEDURE>;
13
13
  path: string[];
14
14
  }
15
- export declare function eachContractProcedureLeaf(options: EachLeafOptions, callback: (options: EachLeafCallbackOptions) => void, result?: EachContractLeafResultItem[], isCurrentRouterContract?: boolean): EachContractLeafResultItem[];
15
+ export declare function forEachContractProcedure(options: EachLeafOptions, callback: (options: EachLeafCallbackOptions) => void, result?: EachContractLeafResultItem[], isCurrentRouterContract?: boolean): EachContractLeafResultItem[];
16
16
  export declare function standardizeHTTPPath(path: HTTPPath): HTTPPath;
17
17
  //# sourceMappingURL=utils.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/openapi",
3
3
  "type": "module",
4
- "version": "0.17.0",
4
+ "version": "0.18.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -35,18 +35,20 @@
35
35
  ],
36
36
  "dependencies": {
37
37
  "@standard-schema/spec": "1.0.0-beta.4",
38
+ "@types/content-disposition": "^0.5.8",
39
+ "content-disposition": "^0.5.4",
38
40
  "escape-string-regexp": "^5.0.0",
41
+ "fast-content-type-parse": "^2.0.0",
42
+ "hono": "^4.6.12",
39
43
  "json-schema-typed": "^8.0.1",
40
44
  "openapi3-ts": "^4.4.0",
41
- "@orpc/shared": "0.17.0",
42
- "@orpc/server": "0.17.0",
43
- "@orpc/transformer": "0.17.0",
44
- "@orpc/zod": "0.17.0",
45
- "@orpc/contract": "0.17.0"
45
+ "wildcard-match": "^5.1.3",
46
+ "@orpc/shared": "0.18.0",
47
+ "@orpc/contract": "0.18.0",
48
+ "@orpc/server": "0.18.0"
46
49
  },
47
50
  "devDependencies": {
48
51
  "@readme/openapi-parser": "^2.6.0",
49
- "hono": "^4.6.12",
50
52
  "zod": "^3.24.1"
51
53
  },
52
54
  "scripts": {
@@ -1,54 +0,0 @@
1
- // src/utils.ts
2
- import { isContractProcedure } from "@orpc/contract";
3
- import { flatLazy, getRouterContract, isLazy, isProcedure } from "@orpc/server";
4
- function eachContractProcedureLeaf(options, callback, result = [], isCurrentRouterContract = false) {
5
- const hiddenContract = getRouterContract(options.router);
6
- if (!isCurrentRouterContract && hiddenContract) {
7
- return eachContractProcedureLeaf(
8
- {
9
- path: options.path,
10
- router: hiddenContract
11
- },
12
- callback,
13
- result,
14
- true
15
- );
16
- }
17
- if (isLazy(options.router)) {
18
- result.push({
19
- lazy: flatLazy(options.router),
20
- path: options.path
21
- });
22
- } else if (isProcedure(options.router)) {
23
- callback({
24
- contract: options.router["~orpc"].contract,
25
- path: options.path
26
- });
27
- } else if (isContractProcedure(options.router)) {
28
- callback({
29
- contract: options.router,
30
- path: options.path
31
- });
32
- } else {
33
- for (const key in options.router) {
34
- eachContractProcedureLeaf(
35
- {
36
- router: options.router[key],
37
- path: [...options.path, key]
38
- },
39
- callback,
40
- result
41
- );
42
- }
43
- }
44
- return result;
45
- }
46
- function standardizeHTTPPath(path) {
47
- return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
48
- }
49
-
50
- export {
51
- eachContractProcedureLeaf,
52
- standardizeHTTPPath
53
- };
54
- //# sourceMappingURL=chunk-CMRY2Z4J.js.map
@@ -1,13 +0,0 @@
1
- import type { ANY_PROCEDURE, ANY_ROUTER } from '@orpc/server';
2
- import type { FetchHandler } from '@orpc/server/fetch';
3
- import type { Router as HonoRouter } from 'hono/router';
4
- export type ResolveRouter = (router: ANY_ROUTER, method: string, pathname: string) => Promise<{
5
- path: string[];
6
- procedure: ANY_PROCEDURE;
7
- params: Record<string, string>;
8
- } | undefined>;
9
- type Routing = HonoRouter<string[]>;
10
- export declare function createOpenAPIHandler(createHonoRouter: () => Routing): FetchHandler;
11
- export declare function createResolveRouter(createHonoRouter: () => Routing): ResolveRouter;
12
- export {};
13
- //# sourceMappingURL=base-handler.d.ts.map
@@ -1,3 +0,0 @@
1
- import type { FetchHandler } from '@orpc/server/fetch';
2
- export declare function createOpenAPIServerHandler(): FetchHandler;
3
- //# sourceMappingURL=server-handler.d.ts.map
@@ -1,3 +0,0 @@
1
- import type { FetchHandler } from '@orpc/server/fetch';
2
- export declare function createOpenAPIServerlessHandler(): FetchHandler;
3
- //# sourceMappingURL=serverless-handler.d.ts.map