@orpc/openapi 0.0.0-next.ee0aeaf → 0.0.0-next.ee46dab

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 (35) hide show
  1. package/README.md +104 -0
  2. package/dist/adapters/fetch/index.d.mts +11 -0
  3. package/dist/adapters/fetch/index.d.ts +11 -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 +11 -0
  12. package/dist/adapters/node/index.d.ts +11 -0
  13. package/dist/adapters/node/index.mjs +22 -0
  14. package/dist/adapters/standard/index.d.mts +34 -0
  15. package/dist/adapters/standard/index.d.ts +34 -0
  16. package/dist/adapters/standard/index.mjs +7 -0
  17. package/dist/index.d.mts +114 -0
  18. package/dist/index.d.ts +114 -0
  19. package/dist/index.mjs +536 -0
  20. package/dist/shared/openapi.CGZ7t-VN.mjs +17 -0
  21. package/dist/shared/openapi.IfmmOyba.d.mts +8 -0
  22. package/dist/shared/openapi.IfmmOyba.d.ts +8 -0
  23. package/dist/shared/openapi.sdeu0I7N.mjs +146 -0
  24. package/package.json +36 -23
  25. package/dist/chunk-7HD5IZWG.js +0 -53
  26. package/dist/fetch.js +0 -707
  27. package/dist/index.js +0 -4585
  28. package/dist/src/fetch/base-handler.d.ts +0 -13
  29. package/dist/src/fetch/index.d.ts +0 -4
  30. package/dist/src/fetch/server-handler.d.ts +0 -3
  31. package/dist/src/fetch/serverless-handler.d.ts +0 -3
  32. package/dist/src/generator.d.ts +0 -24
  33. package/dist/src/index.d.ts +0 -3
  34. package/dist/src/utils.d.ts +0 -17
  35. package/dist/src/zod-to-json-schema.d.ts +0 -43
@@ -0,0 +1,146 @@
1
+ import { fallbackContractConfig } from '@orpc/contract';
2
+ import { isObject } from '@orpc/shared';
3
+ import { toHttpPath } from '@orpc/client/standard';
4
+ import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@orpc/server';
5
+ import { createRouter, addRoute, findRoute } from 'rou3';
6
+ import { standardizeHTTPPath } from '@orpc/openapi-client/standard';
7
+
8
+ class StandardOpenAPICodec {
9
+ constructor(serializer) {
10
+ this.serializer = serializer;
11
+ }
12
+ async decode(request, params, procedure) {
13
+ const inputStructure = fallbackContractConfig("defaultInputStructure", procedure["~orpc"].route.inputStructure);
14
+ if (inputStructure === "compact") {
15
+ const data = request.method === "GET" ? this.serializer.deserialize(request.url.searchParams) : this.serializer.deserialize(await request.body());
16
+ if (data === void 0) {
17
+ return params;
18
+ }
19
+ if (isObject(data)) {
20
+ return {
21
+ ...params,
22
+ ...data
23
+ };
24
+ }
25
+ return data;
26
+ }
27
+ const deserializeSearchParams = () => {
28
+ return this.serializer.deserialize(request.url.searchParams);
29
+ };
30
+ return {
31
+ params,
32
+ get query() {
33
+ const value = deserializeSearchParams();
34
+ Object.defineProperty(this, "query", { value, writable: true });
35
+ return value;
36
+ },
37
+ set query(value) {
38
+ Object.defineProperty(this, "query", { value, writable: true });
39
+ },
40
+ headers: request.headers,
41
+ body: this.serializer.deserialize(await request.body())
42
+ };
43
+ }
44
+ encode(output, procedure) {
45
+ const successStatus = fallbackContractConfig("defaultSuccessStatus", procedure["~orpc"].route.successStatus);
46
+ const outputStructure = fallbackContractConfig("defaultOutputStructure", procedure["~orpc"].route.outputStructure);
47
+ if (outputStructure === "compact") {
48
+ return {
49
+ status: successStatus,
50
+ headers: {},
51
+ body: this.serializer.serialize(output)
52
+ };
53
+ }
54
+ if (!isObject(output)) {
55
+ throw new Error(
56
+ 'Invalid output structure for "detailed" output. Expected format: { body: any, headers?: Record<string, string | string[] | undefined> }'
57
+ );
58
+ }
59
+ return {
60
+ status: successStatus,
61
+ headers: output.headers ?? {},
62
+ body: this.serializer.serialize(output.body)
63
+ };
64
+ }
65
+ encodeError(error) {
66
+ return {
67
+ status: error.status,
68
+ headers: {},
69
+ body: this.serializer.serialize(error.toJSON(), { outputFormat: "plain" })
70
+ };
71
+ }
72
+ }
73
+
74
+ function toRou3Pattern(path) {
75
+ return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
76
+ }
77
+ function decodeParams(params) {
78
+ return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, decodeURIComponent(value)]));
79
+ }
80
+
81
+ class StandardOpenAPIMatcher {
82
+ tree = createRouter();
83
+ pendingRouters = [];
84
+ init(router, path = []) {
85
+ const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
86
+ const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
87
+ const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
88
+ if (isProcedure(contract)) {
89
+ addRoute(this.tree, method, httpPath, {
90
+ path: path2,
91
+ contract,
92
+ procedure: contract,
93
+ // this mean dev not used contract-first so we can used contract as procedure directly
94
+ router
95
+ });
96
+ } else {
97
+ addRoute(this.tree, method, httpPath, {
98
+ path: path2,
99
+ contract,
100
+ procedure: void 0,
101
+ router
102
+ });
103
+ }
104
+ });
105
+ this.pendingRouters.push(...laziedOptions.map((option) => ({
106
+ ...option,
107
+ httpPathPrefix: toHttpPath(option.path),
108
+ laziedPrefix: getLazyMeta(option.router).prefix
109
+ })));
110
+ }
111
+ async match(method, pathname) {
112
+ if (this.pendingRouters.length) {
113
+ const newPendingRouters = [];
114
+ for (const pendingRouter of this.pendingRouters) {
115
+ if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
116
+ const { default: router } = await unlazy(pendingRouter.router);
117
+ this.init(router, pendingRouter.path);
118
+ } else {
119
+ newPendingRouters.push(pendingRouter);
120
+ }
121
+ }
122
+ this.pendingRouters = newPendingRouters;
123
+ }
124
+ const match = findRoute(this.tree, method, pathname);
125
+ if (!match) {
126
+ return void 0;
127
+ }
128
+ if (!match.data.procedure) {
129
+ const { default: maybeProcedure } = await unlazy(getRouter(match.data.router, match.data.path));
130
+ if (!isProcedure(maybeProcedure)) {
131
+ throw new Error(`
132
+ [Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.data.path)}.
133
+ Ensure that the procedure is correctly defined and matches the expected contract.
134
+ `);
135
+ }
136
+ match.data.procedure = createContractedProcedure(maybeProcedure, match.data.contract);
137
+ }
138
+ return {
139
+ path: match.data.path,
140
+ procedure: match.data.procedure,
141
+ params: match.params ? decodeParams(match.params) : void 0
142
+ };
143
+ }
144
+ }
145
+
146
+ export { StandardOpenAPICodec as S, StandardOpenAPIMatcher as a, decodeParams as d, toRou3Pattern as t };
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.ee0aeaf",
4
+ "version": "0.0.0-next.ee46dab",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,42 +15,55 @@
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/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"
31
+ },
32
+ "./hono": {
33
+ "types": "./dist/adapters/hono/index.d.mts",
34
+ "import": "./dist/adapters/hono/index.mjs",
35
+ "default": "./dist/adapters/hono/index.mjs"
36
+ },
37
+ "./next": {
38
+ "types": "./dist/adapters/next/index.d.mts",
39
+ "import": "./dist/adapters/next/index.mjs",
40
+ "default": "./dist/adapters/next/index.mjs"
26
41
  },
27
- "./🔒/*": {
28
- "types": "./dist/src/*.d.ts"
42
+ "./node": {
43
+ "types": "./dist/adapters/node/index.d.mts",
44
+ "import": "./dist/adapters/node/index.mjs",
45
+ "default": "./dist/adapters/node/index.mjs"
29
46
  }
30
47
  },
31
48
  "files": [
32
- "!**/*.map",
33
- "!**/*.tsbuildinfo",
34
49
  "dist"
35
50
  ],
36
51
  "dependencies": {
37
- "@standard-schema/spec": "1.0.0-beta.4",
38
- "escape-string-regexp": "^5.0.0",
39
52
  "json-schema-typed": "^8.0.1",
40
- "openapi3-ts": "^4.4.0",
41
- "@orpc/contract": "0.0.0-next.ee0aeaf",
42
- "@orpc/server": "0.0.0-next.ee0aeaf",
43
- "@orpc/transformer": "0.0.0-next.ee0aeaf",
44
- "@orpc/zod": "0.0.0-next.ee0aeaf",
45
- "@orpc/shared": "0.0.0-next.ee0aeaf"
53
+ "openapi-types": "^12.1.3",
54
+ "rou3": "^0.5.1",
55
+ "@orpc/client": "0.0.0-next.ee46dab",
56
+ "@orpc/contract": "0.0.0-next.ee46dab",
57
+ "@orpc/openapi-client": "0.0.0-next.ee46dab",
58
+ "@orpc/server": "0.0.0-next.ee46dab",
59
+ "@orpc/shared": "0.0.0-next.ee46dab",
60
+ "@orpc/standard-server": "0.0.0-next.ee46dab"
46
61
  },
47
62
  "devDependencies": {
48
- "@readme/openapi-parser": "^2.6.0",
49
- "hono": "^4.6.12",
50
- "zod": "^3.24.1"
63
+ "zod": "^3.24.2"
51
64
  },
52
65
  "scripts": {
53
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.fetch=src/fetch/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
66
+ "build": "unbuild",
54
67
  "build:watch": "pnpm run build --watch",
55
68
  "type:check": "tsc -b"
56
69
  }
@@ -1,53 +0,0 @@
1
- // src/utils.ts
2
- import { isContractProcedure } from "@orpc/contract";
3
- import { isLazy, isProcedure, ROUTER_CONTRACT_SYMBOL } from "@orpc/server";
4
- function eachContractProcedureLeaf(options, callback, result = [], isCurrentRouterContract = false) {
5
- if (!isCurrentRouterContract && ROUTER_CONTRACT_SYMBOL in options.router && options.router[ROUTER_CONTRACT_SYMBOL]) {
6
- return eachContractProcedureLeaf(
7
- {
8
- path: options.path,
9
- router: options.router[ROUTER_CONTRACT_SYMBOL]
10
- },
11
- callback,
12
- result,
13
- true
14
- );
15
- }
16
- if (isLazy(options.router)) {
17
- result.push({
18
- lazy: options.router,
19
- path: options.path
20
- });
21
- } else if (isProcedure(options.router)) {
22
- callback({
23
- contract: options.router.zz$p.contract,
24
- path: options.path
25
- });
26
- } else if (isContractProcedure(options.router)) {
27
- callback({
28
- contract: options.router,
29
- path: options.path
30
- });
31
- } else {
32
- for (const key in options.router) {
33
- eachContractProcedureLeaf(
34
- {
35
- router: options.router[key],
36
- path: [...options.path, key]
37
- },
38
- callback,
39
- result
40
- );
41
- }
42
- }
43
- return result;
44
- }
45
- function standardizeHTTPPath(path) {
46
- return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
47
- }
48
-
49
- export {
50
- eachContractProcedureLeaf,
51
- standardizeHTTPPath
52
- };
53
- //# sourceMappingURL=chunk-7HD5IZWG.js.map