@orpc/openapi 0.0.0-next.67ca77c → 0.0.0-next.68378b4

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.
Files changed (56) hide show
  1. package/README.md +91 -0
  2. package/dist/adapters/fetch/index.d.mts +13 -0
  3. package/dist/adapters/fetch/index.d.ts +13 -0
  4. package/dist/adapters/fetch/index.mjs +10 -0
  5. package/dist/adapters/hono/index.d.mts +6 -0
  6. package/dist/adapters/hono/index.d.ts +6 -0
  7. package/dist/adapters/hono/index.mjs +10 -0
  8. package/dist/adapters/next/index.d.mts +6 -0
  9. package/dist/adapters/next/index.d.ts +6 -0
  10. package/dist/adapters/next/index.mjs +10 -0
  11. package/dist/adapters/node/index.d.mts +13 -0
  12. package/dist/adapters/node/index.d.ts +13 -0
  13. package/dist/adapters/node/index.mjs +32 -0
  14. package/dist/adapters/standard/index.d.mts +23 -0
  15. package/dist/adapters/standard/index.d.ts +23 -0
  16. package/dist/adapters/standard/index.mjs +6 -0
  17. package/dist/index.d.mts +169 -0
  18. package/dist/index.d.ts +169 -0
  19. package/dist/{index.js → index.mjs} +271 -106
  20. package/dist/shared/openapi.B6uueFtN.mjs +29 -0
  21. package/dist/shared/openapi.BHG_gu5Z.mjs +8 -0
  22. package/dist/shared/openapi.C_biOx82.mjs +147 -0
  23. package/package.json +35 -28
  24. package/dist/chunk-KNYXLM77.js +0 -107
  25. package/dist/chunk-WNX6GP4X.js +0 -652
  26. package/dist/fetch.js +0 -46
  27. package/dist/node.js +0 -46
  28. package/dist/src/adapters/fetch/bracket-notation.d.ts +0 -84
  29. package/dist/src/adapters/fetch/index.d.ts +0 -10
  30. package/dist/src/adapters/fetch/input-structure-compact.d.ts +0 -6
  31. package/dist/src/adapters/fetch/input-structure-detailed.d.ts +0 -11
  32. package/dist/src/adapters/fetch/openapi-handler-server.d.ts +0 -7
  33. package/dist/src/adapters/fetch/openapi-handler-serverless.d.ts +0 -7
  34. package/dist/src/adapters/fetch/openapi-handler.d.ts +0 -33
  35. package/dist/src/adapters/fetch/openapi-payload-codec.d.ts +0 -15
  36. package/dist/src/adapters/fetch/openapi-procedure-matcher.d.ts +0 -19
  37. package/dist/src/adapters/fetch/schema-coercer.d.ts +0 -10
  38. package/dist/src/adapters/node/index.d.ts +0 -5
  39. package/dist/src/adapters/node/openapi-handler-server.d.ts +0 -7
  40. package/dist/src/adapters/node/openapi-handler-serverless.d.ts +0 -7
  41. package/dist/src/adapters/node/openapi-handler.d.ts +0 -12
  42. package/dist/src/adapters/node/types.d.ts +0 -2
  43. package/dist/src/index.d.ts +0 -12
  44. package/dist/src/json-serializer.d.ts +0 -5
  45. package/dist/src/openapi-content-builder.d.ts +0 -10
  46. package/dist/src/openapi-error.d.ts +0 -3
  47. package/dist/src/openapi-generator.d.ts +0 -60
  48. package/dist/src/openapi-input-structure-parser.d.ts +0 -22
  49. package/dist/src/openapi-output-structure-parser.d.ts +0 -18
  50. package/dist/src/openapi-parameters-builder.d.ts +0 -12
  51. package/dist/src/openapi-path-parser.d.ts +0 -8
  52. package/dist/src/openapi.d.ts +0 -3
  53. package/dist/src/schema-converter.d.ts +0 -16
  54. package/dist/src/schema-utils.d.ts +0 -11
  55. package/dist/src/schema.d.ts +0 -12
  56. package/dist/src/utils.d.ts +0 -18
@@ -0,0 +1,147 @@
1
+ import { fallbackContractConfig } from '@orpc/contract';
2
+ import { isObject } from '@orpc/shared';
3
+ import { eachContractProcedure, convertPathToHttpPath, isProcedure, getLazyRouterPrefix, unlazy, getRouterChild, createContractedProcedure } from '@orpc/server';
4
+ import { createRouter, addRoute, findRoute } from 'rou3';
5
+ import { s as standardizeHTTPPath } from './openapi.BHG_gu5Z.mjs';
6
+
7
+ class OpenAPICodec {
8
+ constructor(serializer) {
9
+ this.serializer = serializer;
10
+ }
11
+ async decode(request, params, procedure) {
12
+ const inputStructure = fallbackContractConfig("defaultInputStructure", procedure["~orpc"].route.inputStructure);
13
+ if (inputStructure === "compact") {
14
+ const data = request.method === "GET" ? this.serializer.deserialize(request.url.searchParams) : this.serializer.deserialize(await request.body());
15
+ if (data === void 0) {
16
+ return params;
17
+ }
18
+ if (isObject(data)) {
19
+ return {
20
+ ...params,
21
+ ...data
22
+ };
23
+ }
24
+ return data;
25
+ }
26
+ const deserializeSearchParams = () => {
27
+ return this.serializer.deserialize(request.url.searchParams);
28
+ };
29
+ return {
30
+ params,
31
+ get query() {
32
+ const value = deserializeSearchParams();
33
+ Object.defineProperty(this, "query", { value, writable: true });
34
+ return value;
35
+ },
36
+ set query(value) {
37
+ Object.defineProperty(this, "query", { value, writable: true });
38
+ },
39
+ headers: request.headers,
40
+ body: this.serializer.deserialize(await request.body())
41
+ };
42
+ }
43
+ encode(output, procedure) {
44
+ const successStatus = fallbackContractConfig("defaultSuccessStatus", procedure["~orpc"].route.successStatus);
45
+ const outputStructure = fallbackContractConfig("defaultOutputStructure", procedure["~orpc"].route.outputStructure);
46
+ if (outputStructure === "compact") {
47
+ return {
48
+ status: successStatus,
49
+ headers: {},
50
+ body: this.serializer.serialize(output)
51
+ };
52
+ }
53
+ if (!isObject(output)) {
54
+ throw new Error(
55
+ 'Invalid output structure for "detailed" output. Expected format: { body: any, headers?: Record<string, string | string[] | undefined> }'
56
+ );
57
+ }
58
+ return {
59
+ status: successStatus,
60
+ headers: output.headers ?? {},
61
+ body: this.serializer.serialize(output.body)
62
+ };
63
+ }
64
+ encodeError(error) {
65
+ return {
66
+ status: error.status,
67
+ headers: {},
68
+ body: this.serializer.serialize(error.toJSON())
69
+ };
70
+ }
71
+ }
72
+
73
+ class OpenAPIMatcher {
74
+ tree = createRouter();
75
+ pendingRouters = [];
76
+ init(router, path = []) {
77
+ const laziedOptions = eachContractProcedure({
78
+ router,
79
+ path
80
+ }, ({ path: path2, contract }) => {
81
+ const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
82
+ const httpPath = contract["~orpc"].route.path ? toRou3Pattern(contract["~orpc"].route.path) : convertPathToHttpPath(path2);
83
+ if (isProcedure(contract)) {
84
+ addRoute(this.tree, method, httpPath, {
85
+ path: path2,
86
+ contract,
87
+ procedure: contract,
88
+ // this mean dev not used contract-first so we can used contract as procedure directly
89
+ router
90
+ });
91
+ } else {
92
+ addRoute(this.tree, method, httpPath, {
93
+ path: path2,
94
+ contract,
95
+ procedure: void 0,
96
+ router
97
+ });
98
+ }
99
+ });
100
+ this.pendingRouters.push(...laziedOptions.map((option) => ({
101
+ ...option,
102
+ httpPathPrefix: convertPathToHttpPath(option.path),
103
+ laziedPrefix: getLazyRouterPrefix(option.lazied)
104
+ })));
105
+ }
106
+ async match(method, pathname) {
107
+ if (this.pendingRouters.length) {
108
+ const newPendingRouters = [];
109
+ for (const pendingRouter of this.pendingRouters) {
110
+ if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
111
+ const { default: router } = await unlazy(pendingRouter.lazied);
112
+ this.init(router, pendingRouter.path);
113
+ } else {
114
+ newPendingRouters.push(pendingRouter);
115
+ }
116
+ }
117
+ this.pendingRouters = newPendingRouters;
118
+ }
119
+ const match = findRoute(this.tree, method, pathname);
120
+ if (!match) {
121
+ return void 0;
122
+ }
123
+ if (!match.data.procedure) {
124
+ const { default: maybeProcedure } = await unlazy(getRouterChild(match.data.router, ...match.data.path));
125
+ if (!isProcedure(maybeProcedure)) {
126
+ throw new Error(`
127
+ [Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.data.path)}.
128
+ Ensure that the procedure is correctly defined and matches the expected contract.
129
+ `);
130
+ }
131
+ match.data.procedure = createContractedProcedure(match.data.contract, maybeProcedure);
132
+ }
133
+ return {
134
+ path: match.data.path,
135
+ procedure: match.data.procedure,
136
+ params: match.params ? decodeParams(match.params) : void 0
137
+ };
138
+ }
139
+ }
140
+ function toRou3Pattern(path) {
141
+ return standardizeHTTPPath(path).replace(/\{\+([^}]+)\}/g, "**:$1").replace(/\{([^}]+)\}/g, ":$1");
142
+ }
143
+ function decodeParams(params) {
144
+ return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, decodeURIComponent(value)]));
145
+ }
146
+
147
+ export { OpenAPICodec as O, OpenAPIMatcher as a };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/openapi",
3
3
  "type": "module",
4
- "version": "0.0.0-next.67ca77c",
4
+ "version": "0.0.0-next.68378b4",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,50 +15,57 @@
15
15
  ],
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/src/index.d.ts",
19
- "import": "./dist/index.js",
20
- "default": "./dist/index.js"
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs",
20
+ "default": "./dist/index.mjs"
21
+ },
22
+ "./standard": {
23
+ "types": "./dist/adapters/standard/index.d.mts",
24
+ "import": "./dist/adapters/standard/index.mjs",
25
+ "default": "./dist/adapters/standard/index.mjs"
21
26
  },
22
27
  "./fetch": {
23
- "types": "./dist/src/adapters/fetch/index.d.ts",
24
- "import": "./dist/fetch.js",
25
- "default": "./dist/fetch.js"
28
+ "types": "./dist/adapters/fetch/index.d.mts",
29
+ "import": "./dist/adapters/fetch/index.mjs",
30
+ "default": "./dist/adapters/fetch/index.mjs"
26
31
  },
27
- "./node": {
28
- "types": "./dist/src/adapters/node/index.d.ts",
29
- "import": "./dist/node.js",
30
- "default": "./dist/node.js"
32
+ "./hono": {
33
+ "types": "./dist/adapters/hono/index.d.mts",
34
+ "import": "./dist/adapters/hono/index.mjs",
35
+ "default": "./dist/adapters/hono/index.mjs"
31
36
  },
32
- "./🔒/*": {
33
- "types": "./dist/src/*.d.ts"
37
+ "./next": {
38
+ "types": "./dist/adapters/next/index.d.mts",
39
+ "import": "./dist/adapters/next/index.mjs",
40
+ "default": "./dist/adapters/next/index.mjs"
41
+ },
42
+ "./node": {
43
+ "types": "./dist/adapters/node/index.d.mts",
44
+ "import": "./dist/adapters/node/index.mjs",
45
+ "default": "./dist/adapters/node/index.mjs"
34
46
  }
35
47
  },
36
48
  "files": [
37
- "!**/*.map",
38
- "!**/*.tsbuildinfo",
39
49
  "dist"
40
50
  ],
41
51
  "dependencies": {
42
- "@mjackson/node-fetch-server": "^0.5.0",
43
- "@standard-schema/spec": "1.0.0-beta.4",
44
- "@types/content-disposition": "^0.5.8",
45
- "content-disposition": "^0.5.4",
46
- "escape-string-regexp": "^5.0.0",
47
- "fast-content-type-parse": "^2.0.0",
48
- "hono": "^4.6.12",
49
52
  "json-schema-typed": "^8.0.1",
50
53
  "openapi3-ts": "^4.4.0",
51
- "wildcard-match": "^5.1.3",
52
- "@orpc/contract": "0.0.0-next.67ca77c",
53
- "@orpc/server": "0.0.0-next.67ca77c",
54
- "@orpc/shared": "0.0.0-next.67ca77c"
54
+ "rou3": "^0.5.1",
55
+ "@orpc/client": "0.0.0-next.68378b4",
56
+ "@orpc/openapi-client": "0.0.0-next.68378b4",
57
+ "@orpc/contract": "0.0.0-next.68378b4",
58
+ "@orpc/server": "0.0.0-next.68378b4",
59
+ "@orpc/shared": "0.0.0-next.68378b4",
60
+ "@orpc/standard-server": "0.0.0-next.68378b4",
61
+ "@orpc/standard-server-fetch": "0.0.0-next.68378b4",
62
+ "@orpc/standard-server-node": "0.0.0-next.68378b4"
55
63
  },
56
64
  "devDependencies": {
57
- "@readme/openapi-parser": "^2.6.0",
58
65
  "zod": "^3.24.1"
59
66
  },
60
67
  "scripts": {
61
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.fetch=src/adapters/fetch/index.ts --entry.node=src/adapters/node/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
68
+ "build": "unbuild",
62
69
  "build:watch": "pnpm run build --watch",
63
70
  "type:check": "tsc -b"
64
71
  }
@@ -1,107 +0,0 @@
1
- // src/json-serializer.ts
2
- import { isPlainObject } from "@orpc/shared";
3
- var JSONSerializer = class {
4
- serialize(payload) {
5
- if (payload instanceof Set)
6
- return this.serialize([...payload]);
7
- if (payload instanceof Map)
8
- return this.serialize([...payload.entries()]);
9
- if (Array.isArray(payload)) {
10
- return payload.map((v) => v === void 0 ? "undefined" : this.serialize(v));
11
- }
12
- if (Number.isNaN(payload))
13
- return "NaN";
14
- if (typeof payload === "bigint")
15
- return payload.toString();
16
- if (payload instanceof Date && Number.isNaN(payload.getTime())) {
17
- return "Invalid Date";
18
- }
19
- if (payload instanceof RegExp)
20
- return payload.toString();
21
- if (payload instanceof URL)
22
- return payload.toString();
23
- if (!isPlainObject(payload))
24
- return payload;
25
- return Object.keys(payload).reduce(
26
- (carry, key) => {
27
- const val = payload[key];
28
- carry[key] = this.serialize(val);
29
- return carry;
30
- },
31
- {}
32
- );
33
- }
34
- };
35
-
36
- // src/utils.ts
37
- import { isContractProcedure } from "@orpc/contract";
38
- import { getRouterContract, isLazy, isProcedure, unlazy } from "@orpc/server";
39
- function forEachContractProcedure(options, callback, result = [], isCurrentRouterContract = false) {
40
- const hiddenContract = getRouterContract(options.router);
41
- if (!isCurrentRouterContract && hiddenContract) {
42
- return forEachContractProcedure(
43
- {
44
- path: options.path,
45
- router: hiddenContract
46
- },
47
- callback,
48
- result,
49
- true
50
- );
51
- }
52
- if (isLazy(options.router)) {
53
- result.push({
54
- router: options.router,
55
- path: options.path
56
- });
57
- } else if (isProcedure(options.router)) {
58
- callback({
59
- contract: options.router["~orpc"].contract,
60
- path: options.path
61
- });
62
- } else if (isContractProcedure(options.router)) {
63
- callback({
64
- contract: options.router,
65
- path: options.path
66
- });
67
- } else {
68
- for (const key in options.router) {
69
- forEachContractProcedure(
70
- {
71
- router: options.router[key],
72
- path: [...options.path, key]
73
- },
74
- callback,
75
- result
76
- );
77
- }
78
- }
79
- return result;
80
- }
81
- async function forEachAllContractProcedure(router, callback) {
82
- const pending = [{
83
- path: [],
84
- router
85
- }];
86
- for (const item of pending) {
87
- const lazies = forEachContractProcedure(item, callback);
88
- for (const lazy of lazies) {
89
- const { default: router2 } = await unlazy(lazy.router);
90
- pending.push({
91
- path: lazy.path,
92
- router: router2
93
- });
94
- }
95
- }
96
- }
97
- function standardizeHTTPPath(path) {
98
- return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
99
- }
100
-
101
- export {
102
- JSONSerializer,
103
- forEachContractProcedure,
104
- forEachAllContractProcedure,
105
- standardizeHTTPPath
106
- };
107
- //# sourceMappingURL=chunk-KNYXLM77.js.map