@orpc/server 0.0.0-next.1d55ec0 → 0.0.0-next.1ea8333

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 (50) hide show
  1. package/README.md +122 -0
  2. package/dist/adapters/fetch/index.d.mts +28 -0
  3. package/dist/adapters/fetch/index.d.ts +28 -0
  4. package/dist/adapters/fetch/index.mjs +9 -0
  5. package/dist/adapters/hono/index.d.mts +21 -0
  6. package/dist/adapters/hono/index.d.ts +21 -0
  7. package/dist/adapters/hono/index.mjs +32 -0
  8. package/dist/adapters/next/index.d.mts +28 -0
  9. package/dist/adapters/next/index.d.ts +28 -0
  10. package/dist/adapters/next/index.mjs +29 -0
  11. package/dist/adapters/node/index.d.mts +36 -0
  12. package/dist/adapters/node/index.d.ts +36 -0
  13. package/dist/adapters/node/index.mjs +32 -0
  14. package/dist/adapters/standard/index.d.mts +26 -0
  15. package/dist/adapters/standard/index.d.ts +26 -0
  16. package/dist/adapters/standard/index.mjs +6 -0
  17. package/dist/index.d.mts +277 -0
  18. package/dist/index.d.ts +277 -0
  19. package/dist/index.mjs +342 -0
  20. package/dist/plugins/index.d.mts +31 -0
  21. package/dist/plugins/index.d.ts +31 -0
  22. package/dist/plugins/index.mjs +103 -0
  23. package/dist/shared/server.BY9sDlwl.mjs +158 -0
  24. package/dist/shared/server.BqBN5WhH.d.mts +8 -0
  25. package/dist/shared/server.BtxZnWJ9.mjs +374 -0
  26. package/dist/shared/server.CPqNKiJp.d.ts +75 -0
  27. package/dist/shared/server.Dba3Iiyp.mjs +12 -0
  28. package/dist/shared/server.Del5OmaY.mjs +29 -0
  29. package/dist/shared/server.Dm3ZuTuI.d.ts +8 -0
  30. package/dist/shared/server.MZvbGc3n.d.mts +143 -0
  31. package/dist/shared/server.MZvbGc3n.d.ts +143 -0
  32. package/dist/shared/server.P4_D9lKb.d.mts +75 -0
  33. package/package.json +41 -17
  34. package/dist/chunk-CVLK2PBB.js +0 -189
  35. package/dist/fetch.js +0 -680
  36. package/dist/index.js +0 -393
  37. package/dist/src/adapters/fetch.d.ts +0 -41
  38. package/dist/src/builder.d.ts +0 -48
  39. package/dist/src/index.d.ts +0 -14
  40. package/dist/src/middleware.d.ts +0 -25
  41. package/dist/src/procedure-builder.d.ts +0 -30
  42. package/dist/src/procedure-caller.d.ts +0 -18
  43. package/dist/src/procedure-implementer.d.ts +0 -17
  44. package/dist/src/procedure.d.ts +0 -28
  45. package/dist/src/router-builder.d.ts +0 -21
  46. package/dist/src/router-caller.d.ts +0 -21
  47. package/dist/src/router-implementer.d.ts +0 -19
  48. package/dist/src/router.d.ts +0 -19
  49. package/dist/src/types.d.ts +0 -7
  50. package/dist/src/utils.d.ts +0 -2
@@ -0,0 +1,31 @@
1
+ import { b as StandardHandlerInterceptorOptions, H as HandlerPlugin, a as StandardHandlerOptions } from '../shared/server.P4_D9lKb.mjs';
2
+ export { C as CompositeHandlerPlugin } from '../shared/server.P4_D9lKb.mjs';
3
+ import { Value } from '@orpc/shared';
4
+ import { C as Context } from '../shared/server.MZvbGc3n.mjs';
5
+ import '@orpc/contract';
6
+ import '@orpc/standard-server';
7
+ import '@orpc/client';
8
+
9
+ interface CORSOptions<TContext extends Context> {
10
+ origin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
11
+ timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
12
+ allowMethods?: readonly string[];
13
+ allowHeaders?: readonly string[];
14
+ maxAge?: number;
15
+ credentials?: boolean;
16
+ exposeHeaders?: readonly string[];
17
+ }
18
+ declare class CORSPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
19
+ private readonly options;
20
+ constructor(options?: CORSOptions<TContext>);
21
+ init(options: StandardHandlerOptions<TContext>): void;
22
+ }
23
+
24
+ interface ResponseHeadersPluginContext {
25
+ resHeaders?: Headers;
26
+ }
27
+ declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements HandlerPlugin<TContext> {
28
+ init(options: StandardHandlerOptions<TContext>): void;
29
+ }
30
+
31
+ export { type CORSOptions, CORSPlugin, HandlerPlugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
@@ -0,0 +1,31 @@
1
+ import { b as StandardHandlerInterceptorOptions, H as HandlerPlugin, a as StandardHandlerOptions } from '../shared/server.CPqNKiJp.js';
2
+ export { C as CompositeHandlerPlugin } from '../shared/server.CPqNKiJp.js';
3
+ import { Value } from '@orpc/shared';
4
+ import { C as Context } from '../shared/server.MZvbGc3n.js';
5
+ import '@orpc/contract';
6
+ import '@orpc/standard-server';
7
+ import '@orpc/client';
8
+
9
+ interface CORSOptions<TContext extends Context> {
10
+ origin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
11
+ timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
12
+ allowMethods?: readonly string[];
13
+ allowHeaders?: readonly string[];
14
+ maxAge?: number;
15
+ credentials?: boolean;
16
+ exposeHeaders?: readonly string[];
17
+ }
18
+ declare class CORSPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
19
+ private readonly options;
20
+ constructor(options?: CORSOptions<TContext>);
21
+ init(options: StandardHandlerOptions<TContext>): void;
22
+ }
23
+
24
+ interface ResponseHeadersPluginContext {
25
+ resHeaders?: Headers;
26
+ }
27
+ declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements HandlerPlugin<TContext> {
28
+ init(options: StandardHandlerOptions<TContext>): void;
29
+ }
30
+
31
+ export { type CORSOptions, CORSPlugin, HandlerPlugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
@@ -0,0 +1,103 @@
1
+ export { C as CompositeHandlerPlugin } from '../shared/server.Dba3Iiyp.mjs';
2
+ import { value } from '@orpc/shared';
3
+
4
+ class CORSPlugin {
5
+ options;
6
+ constructor(options = {}) {
7
+ const defaults = {
8
+ origin: (origin) => origin,
9
+ allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"]
10
+ };
11
+ this.options = {
12
+ ...defaults,
13
+ ...options
14
+ };
15
+ }
16
+ init(options) {
17
+ options.rootInterceptors ??= [];
18
+ options.rootInterceptors.unshift(async (interceptorOptions) => {
19
+ if (interceptorOptions.request.method === "OPTIONS") {
20
+ const resHeaders = {};
21
+ if (this.options.maxAge !== void 0) {
22
+ resHeaders["access-control-max-age"] = this.options.maxAge.toString();
23
+ }
24
+ if (this.options.allowMethods?.length) {
25
+ resHeaders["access-control-allow-methods"] = this.options.allowMethods.join(",");
26
+ }
27
+ const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers["access-control-request-headers"];
28
+ if (Array.isArray(allowHeaders) && allowHeaders.length) {
29
+ resHeaders["access-control-allow-headers"] = allowHeaders.join(",");
30
+ } else if (typeof allowHeaders === "string") {
31
+ resHeaders["access-control-allow-headers"] = allowHeaders;
32
+ }
33
+ return {
34
+ matched: true,
35
+ response: {
36
+ status: 204,
37
+ headers: resHeaders,
38
+ body: void 0
39
+ }
40
+ };
41
+ }
42
+ return interceptorOptions.next();
43
+ });
44
+ options.rootInterceptors.unshift(async (interceptorOptions) => {
45
+ const result = await interceptorOptions.next();
46
+ if (!result.matched) {
47
+ return result;
48
+ }
49
+ const origin = Array.isArray(interceptorOptions.request.headers.origin) ? interceptorOptions.request.headers.origin.join(",") : interceptorOptions.request.headers.origin || "";
50
+ const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
51
+ const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
52
+ if (allowedOriginArr.includes("*")) {
53
+ result.response.headers["access-control-allow-origin"] = "*";
54
+ } else {
55
+ if (allowedOriginArr.includes(origin)) {
56
+ result.response.headers["access-control-allow-origin"] = origin;
57
+ }
58
+ result.response.headers.vary = interceptorOptions.request.headers.vary ?? "origin";
59
+ }
60
+ const allowedTimingOrigin = await value(this.options.timingOrigin, origin, interceptorOptions);
61
+ const allowedTimingOriginArr = Array.isArray(allowedTimingOrigin) ? allowedTimingOrigin : [allowedTimingOrigin];
62
+ if (allowedTimingOriginArr.includes("*")) {
63
+ result.response.headers["timing-allow-origin"] = "*";
64
+ } else if (allowedTimingOriginArr.includes(origin)) {
65
+ result.response.headers["timing-allow-origin"] = origin;
66
+ }
67
+ if (this.options.credentials) {
68
+ result.response.headers["access-control-allow-credentials"] = "true";
69
+ }
70
+ if (this.options.exposeHeaders?.length) {
71
+ result.response.headers["access-control-expose-headers"] = this.options.exposeHeaders.join(",");
72
+ }
73
+ return result;
74
+ });
75
+ }
76
+ }
77
+
78
+ class ResponseHeadersPlugin {
79
+ init(options) {
80
+ options.rootInterceptors ??= [];
81
+ options.rootInterceptors.push(async (interceptorOptions) => {
82
+ const headers = new Headers();
83
+ interceptorOptions.context.resHeaders = headers;
84
+ const result = await interceptorOptions.next();
85
+ if (!result.matched) {
86
+ return result;
87
+ }
88
+ const responseHeaders = result.response.headers;
89
+ for (const [key, value] of headers) {
90
+ if (Array.isArray(responseHeaders[key])) {
91
+ responseHeaders[key].push(value);
92
+ } else if (responseHeaders[key] !== void 0) {
93
+ responseHeaders[key] = [responseHeaders[key], value];
94
+ } else {
95
+ responseHeaders[key] = value;
96
+ }
97
+ }
98
+ return result;
99
+ });
100
+ }
101
+ }
102
+
103
+ export { CORSPlugin, ResponseHeadersPlugin };
@@ -0,0 +1,158 @@
1
+ import { ORPCError, toORPCError } from '@orpc/client';
2
+ import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
3
+ import { C as CompositeHandlerPlugin } from './server.Dba3Iiyp.mjs';
4
+ import { c as createProcedureClient, t as traverseContractProcedures, a as toHttpPath, i as isProcedure, u as unlazy, g as getRouter, b as createContractedProcedure } from './server.BtxZnWJ9.mjs';
5
+
6
+ class StandardHandler {
7
+ constructor(router, matcher, codec, options) {
8
+ this.matcher = matcher;
9
+ this.codec = codec;
10
+ this.options = options;
11
+ this.plugin = new CompositeHandlerPlugin(options.plugins);
12
+ this.plugin.init(this.options);
13
+ this.matcher.init(router);
14
+ }
15
+ plugin;
16
+ handle(request, ...[options]) {
17
+ return intercept(
18
+ this.options.rootInterceptors ?? [],
19
+ {
20
+ request,
21
+ ...options,
22
+ context: options?.context ?? {}
23
+ // context is optional only when all fields are optional so we can safely force it to have a context
24
+ },
25
+ async (interceptorOptions) => {
26
+ let isDecoding = false;
27
+ try {
28
+ return await intercept(
29
+ this.options.interceptors ?? [],
30
+ interceptorOptions,
31
+ async (interceptorOptions2) => {
32
+ const method = interceptorOptions2.request.method;
33
+ const url = interceptorOptions2.request.url;
34
+ const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
35
+ const match = await this.matcher.match(method, pathname);
36
+ if (!match) {
37
+ return { matched: false, response: void 0 };
38
+ }
39
+ const client = createProcedureClient(match.procedure, {
40
+ context: interceptorOptions2.context,
41
+ path: match.path,
42
+ interceptors: this.options.clientInterceptors
43
+ });
44
+ isDecoding = true;
45
+ const input = await this.codec.decode(request, match.params, match.procedure);
46
+ isDecoding = false;
47
+ const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
48
+ const output = await client(input, { signal: request.signal, lastEventId });
49
+ const response = this.codec.encode(output, match.procedure);
50
+ return {
51
+ matched: true,
52
+ response
53
+ };
54
+ }
55
+ );
56
+ } catch (e) {
57
+ const error = isDecoding ? new ORPCError("BAD_REQUEST", {
58
+ message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
59
+ cause: e
60
+ }) : toORPCError(e);
61
+ const response = this.codec.encodeError(error);
62
+ return {
63
+ matched: true,
64
+ response
65
+ };
66
+ }
67
+ }
68
+ );
69
+ }
70
+ }
71
+
72
+ class StandardRPCCodec {
73
+ constructor(serializer) {
74
+ this.serializer = serializer;
75
+ }
76
+ async decode(request, _params, _procedure) {
77
+ const serialized = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("data").at(-1)) : await request.body();
78
+ return this.serializer.deserialize(serialized);
79
+ }
80
+ encode(output, _procedure) {
81
+ return {
82
+ status: 200,
83
+ headers: {},
84
+ body: this.serializer.serialize(output)
85
+ };
86
+ }
87
+ encodeError(error) {
88
+ return {
89
+ status: error.status,
90
+ headers: {},
91
+ body: this.serializer.serialize(error.toJSON())
92
+ };
93
+ }
94
+ }
95
+
96
+ class StandardRPCMatcher {
97
+ tree = {};
98
+ pendingRouters = [];
99
+ init(router, path = []) {
100
+ const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
101
+ const httpPath = toHttpPath(path2);
102
+ if (isProcedure(contract)) {
103
+ this.tree[httpPath] = {
104
+ path: path2,
105
+ contract,
106
+ procedure: contract,
107
+ // this mean dev not used contract-first so we can used contract as procedure directly
108
+ router
109
+ };
110
+ } else {
111
+ this.tree[httpPath] = {
112
+ path: path2,
113
+ contract,
114
+ procedure: void 0,
115
+ router
116
+ };
117
+ }
118
+ });
119
+ this.pendingRouters.push(...laziedOptions.map((option) => ({
120
+ ...option,
121
+ httpPathPrefix: toHttpPath(option.path)
122
+ })));
123
+ }
124
+ async match(_method, pathname) {
125
+ if (this.pendingRouters.length) {
126
+ const newPendingRouters = [];
127
+ for (const pendingRouter of this.pendingRouters) {
128
+ if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
129
+ const { default: router } = await unlazy(pendingRouter.router);
130
+ this.init(router, pendingRouter.path);
131
+ } else {
132
+ newPendingRouters.push(pendingRouter);
133
+ }
134
+ }
135
+ this.pendingRouters = newPendingRouters;
136
+ }
137
+ const match = this.tree[pathname];
138
+ if (!match) {
139
+ return void 0;
140
+ }
141
+ if (!match.procedure) {
142
+ const { default: maybeProcedure } = await unlazy(getRouter(match.router, match.path));
143
+ if (!isProcedure(maybeProcedure)) {
144
+ throw new Error(`
145
+ [Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.path)}.
146
+ Ensure that the procedure is correctly defined and matches the expected contract.
147
+ `);
148
+ }
149
+ match.procedure = createContractedProcedure(maybeProcedure, match.contract);
150
+ }
151
+ return {
152
+ path: match.path,
153
+ procedure: match.procedure
154
+ };
155
+ }
156
+ }
157
+
158
+ export { StandardHandler as S, StandardRPCCodec as a, StandardRPCMatcher as b };
@@ -0,0 +1,8 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { C as Context } from './server.MZvbGc3n.mjs';
3
+ import { a as StandardHandlerOptions } from './server.P4_D9lKb.mjs';
4
+
5
+ interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
+ }
7
+
8
+ export type { StandardRPCHandlerOptions as S };