@orpc/server 2.0.0-beta.47 → 2.0.0-beta.49
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/README.md +6 -7
- package/dist/adapters/aws-lambda/handler.d.ts +25 -0
- package/dist/adapters/aws-lambda/handler.js +24 -0
- package/dist/adapters/aws-lambda/index.d.ts +10 -45
- package/dist/adapters/aws-lambda/index.js +2 -0
- package/dist/adapters/aws-lambda/rpc-handler.d.ts +19 -0
- package/dist/adapters/aws-lambda/rpc-handler.js +18 -0
- package/dist/adapters/crossws/handler.d.ts +45 -0
- package/dist/adapters/crossws/handler.js +60 -0
- package/dist/adapters/crossws/index.d.ts +2 -63
- package/dist/adapters/crossws/index.js +2 -0
- package/dist/adapters/crossws/rpc-handler.d.ts +16 -0
- package/dist/adapters/crossws/rpc-handler.js +15 -0
- package/dist/adapters/fastify/handler.d.ts +21 -0
- package/dist/adapters/fastify/handler.js +20 -0
- package/dist/adapters/fastify/index.d.ts +2 -42
- package/dist/adapters/fastify/index.js +2 -0
- package/dist/adapters/fastify/rpc-handler.d.ts +15 -0
- package/dist/adapters/fastify/rpc-handler.js +14 -0
- package/dist/adapters/fetch/handler.d.ts +23 -0
- package/dist/adapters/fetch/handler.js +22 -0
- package/dist/adapters/fetch/index.d.ts +5 -45
- package/dist/adapters/fetch/index.js +7 -0
- package/dist/adapters/fetch/rpc-handler.d.ts +17 -0
- package/dist/adapters/fetch/rpc-handler.js +16 -0
- package/dist/adapters/message-port/{index.d.mts → handler.d.ts} +17 -30
- package/dist/adapters/message-port/handler.js +86 -0
- package/dist/adapters/message-port/index.d.ts +2 -77
- package/dist/adapters/message-port/index.js +2 -0
- package/dist/adapters/message-port/rpc-handler.d.ts +16 -0
- package/dist/adapters/message-port/rpc-handler.js +15 -0
- package/dist/adapters/node/handler.d.ts +21 -0
- package/dist/adapters/node/handler.js +20 -0
- package/dist/adapters/node/index.d.ts +5 -42
- package/dist/adapters/node/index.js +7 -0
- package/dist/adapters/node/rpc-handler.d.ts +16 -0
- package/dist/adapters/node/rpc-handler.js +15 -0
- package/dist/adapters/standard/codec.d.ts +16 -0
- package/dist/adapters/standard/codec.js +1 -0
- package/dist/adapters/standard/handler.d.ts +68 -0
- package/dist/adapters/standard/handler.js +205 -0
- package/dist/adapters/standard/index.d.ts +6 -95
- package/dist/adapters/standard/index.js +5 -0
- package/dist/adapters/standard/plugin.d.ts +32 -0
- package/dist/adapters/standard/plugin.js +16 -0
- package/dist/adapters/standard/rpc-handler-codec.d.ts +46 -0
- package/dist/adapters/standard/rpc-handler-codec.js +51 -0
- package/dist/adapters/standard/rpc-matcher.d.ts +53 -0
- package/dist/adapters/standard/rpc-matcher.js +152 -0
- package/dist/adapters/standard/utils.d.ts +10 -0
- package/dist/adapters/standard/utils.js +6 -0
- package/dist/adapters/standard-peer/index.d.ts +1 -11
- package/dist/adapters/standard-peer/index.js +1 -0
- package/dist/adapters/standard-peer/utils.d.ts +4 -0
- package/dist/adapters/standard-peer/utils.js +7 -0
- package/dist/adapters/websocket/{index.d.mts → handler.d.ts} +11 -29
- package/dist/adapters/websocket/handler.js +85 -0
- package/dist/adapters/websocket/index.d.ts +2 -77
- package/dist/adapters/websocket/index.js +2 -0
- package/dist/adapters/websocket/rpc-handler.d.ts +16 -0
- package/dist/adapters/websocket/rpc-handler.js +15 -0
- package/dist/builder-variants.d.ts +226 -0
- package/dist/builder-variants.js +1 -0
- package/dist/builder.d.ts +127 -0
- package/dist/builder.js +226 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +2 -0
- package/dist/context.d.ts +6 -0
- package/dist/context.js +1 -0
- package/dist/extensions/callable.d.ts +6 -5
- package/dist/extensions/callable.js +7 -0
- package/dist/helpers/base64url.d.ts +31 -0
- package/dist/helpers/base64url.js +53 -0
- package/dist/helpers/cookie.d.ts +51 -0
- package/dist/helpers/cookie.js +69 -0
- package/dist/helpers/encryption.d.ts +28 -0
- package/dist/helpers/encryption.js +80 -0
- package/dist/helpers/index.d.ts +4 -167
- package/dist/helpers/index.js +4 -0
- package/dist/helpers/signing.d.ts +50 -0
- package/dist/helpers/signing.js +88 -0
- package/dist/implementer-procedure.d.ts +16 -0
- package/dist/implementer-procedure.js +44 -0
- package/dist/implementer-router.d.ts +40 -0
- package/dist/implementer-router.js +117 -0
- package/dist/implementer.d.ts +16 -0
- package/dist/implementer.js +37 -0
- package/dist/index.d.ts +45 -460
- package/dist/index.js +48 -0
- package/dist/lazy.d.ts +20 -0
- package/dist/lazy.js +31 -0
- package/dist/middleware-decorated.d.ts +10 -0
- package/dist/middleware-decorated.js +71 -0
- package/dist/middleware.d.ts +54 -0
- package/dist/middleware.js +1 -0
- package/dist/plugins/batch.d.ts +102 -0
- package/dist/plugins/batch.js +405 -0
- package/dist/plugins/cors.d.ts +73 -0
- package/dist/plugins/cors.js +112 -0
- package/dist/plugins/get-method-csrf-protection.d.ts +23 -0
- package/dist/plugins/get-method-csrf-protection.js +64 -0
- package/dist/plugins/head-method.d.ts +23 -0
- package/dist/plugins/head-method.js +104 -0
- package/dist/plugins/index.d.ts +29 -422
- package/dist/plugins/index.js +29 -0
- package/dist/plugins/method-override.d.ts +38 -0
- package/dist/plugins/method-override.js +50 -0
- package/dist/plugins/request-compression.d.ts +16 -0
- package/dist/plugins/request-compression.js +82 -0
- package/dist/plugins/request-headers.d.ts +22 -0
- package/dist/plugins/request-headers.js +30 -0
- package/dist/{shared/server.CgBF3Vj1.d.ts → plugins/request-limit.d.ts} +4 -8
- package/dist/plugins/request-limit.js +68 -0
- package/dist/plugins/response-compression.d.ts +59 -0
- package/dist/plugins/response-compression.js +209 -0
- package/dist/plugins/response-headers.d.ts +30 -0
- package/dist/plugins/response-headers.js +47 -0
- package/dist/plugins/rethrow.d.ts +33 -0
- package/dist/plugins/rethrow.js +59 -0
- package/dist/plugins/signal-sync.d.ts +13 -0
- package/dist/plugins/signal-sync.js +91 -0
- package/dist/plugins/timeout.d.ts +37 -0
- package/dist/plugins/timeout.js +59 -0
- package/dist/procedure-client.d.ts +19 -0
- package/dist/procedure-client.js +175 -0
- package/dist/procedure-decorated.d.ts +10 -0
- package/dist/procedure-decorated.js +44 -0
- package/dist/procedure-utils.d.ts +41 -0
- package/dist/procedure-utils.js +49 -0
- package/dist/procedure.d.ts +72 -0
- package/dist/procedure.js +24 -0
- package/dist/router-client.d.ts +23 -0
- package/dist/router-client.js +43 -0
- package/dist/router-hidden.d.ts +5 -0
- package/dist/router-hidden.js +14 -0
- package/dist/router-utils.d.ts +48 -0
- package/dist/router-utils.js +202 -0
- package/dist/{shared/server.Dz7j9BEh.d.ts → router.d.ts} +14 -15
- package/dist/router.js +1 -0
- package/package.json +84 -86
- package/dist/adapters/aws-lambda/index.d.mts +0 -51
- package/dist/adapters/aws-lambda/index.mjs +0 -39
- package/dist/adapters/crossws/index.d.mts +0 -63
- package/dist/adapters/crossws/index.mjs +0 -68
- package/dist/adapters/fastify/index.d.mts +0 -42
- package/dist/adapters/fastify/index.mjs +0 -39
- package/dist/adapters/fetch/index.d.mts +0 -47
- package/dist/adapters/fetch/index.mjs +0 -42
- package/dist/adapters/message-port/index.mjs +0 -87
- package/dist/adapters/node/index.d.mts +0 -44
- package/dist/adapters/node/index.mjs +0 -41
- package/dist/adapters/standard/index.d.mts +0 -95
- package/dist/adapters/standard/index.mjs +0 -8
- package/dist/adapters/standard-peer/index.d.mts +0 -11
- package/dist/adapters/standard-peer/index.mjs +0 -10
- package/dist/adapters/websocket/index.mjs +0 -87
- package/dist/extensions/callable.d.mts +0 -10
- package/dist/extensions/callable.mjs +0 -11
- package/dist/helpers/index.d.mts +0 -167
- package/dist/helpers/index.mjs +0 -200
- package/dist/index.d.mts +0 -476
- package/dist/index.mjs +0 -463
- package/dist/plugins/index.d.mts +0 -432
- package/dist/plugins/index.mjs +0 -965
- package/dist/shared/server.1ExWFMze.mjs +0 -69
- package/dist/shared/server.9B1jEm5a.d.mts +0 -105
- package/dist/shared/server.BB464y-6.mjs +0 -207
- package/dist/shared/server.BC5BIu02.d.mts +0 -73
- package/dist/shared/server.BSYGskId.d.ts +0 -14
- package/dist/shared/server.BvG3boBj.d.mts +0 -14
- package/dist/shared/server.CTV_mC2P.d.mts +0 -34
- package/dist/shared/server.CYbIpHbH.mjs +0 -239
- package/dist/shared/server.Chq5FpXf.mjs +0 -353
- package/dist/shared/server.DFPxP2v3.d.mts +0 -166
- package/dist/shared/server.DFPxP2v3.d.ts +0 -166
- package/dist/shared/server.DOg7ke6y.d.ts +0 -105
- package/dist/shared/server.W91HSRkE.mjs +0 -9
- package/dist/shared/server.sBQSgqK3.mjs +0 -41
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { decodeBase64url, encodeBase64url } from './base64url.js';
|
|
2
|
+
const PBKDF2_CONFIG = {
|
|
3
|
+
name: 'PBKDF2',
|
|
4
|
+
/**
|
|
5
|
+
* Below the OWASP Password Storage Cheat Sheet recommendation of 600,000 iterations
|
|
6
|
+
* for PBKDF2-HMAC-SHA256. The output does not record the iteration count, so changing
|
|
7
|
+
* it would make existing ciphertexts undecryptable; raising it needs a versioned format.
|
|
8
|
+
*/
|
|
9
|
+
iterations: 60_000,
|
|
10
|
+
hash: 'SHA-256',
|
|
11
|
+
};
|
|
12
|
+
const AES_GCM_CONFIG = {
|
|
13
|
+
name: 'AES-GCM',
|
|
14
|
+
length: 256,
|
|
15
|
+
};
|
|
16
|
+
const CRYPTO_CONSTANTS = {
|
|
17
|
+
SALT_LENGTH: 16,
|
|
18
|
+
IV_LENGTH: 12,
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Encrypts a string using AES-GCM with a secret key.
|
|
22
|
+
* The output is base64url encoded to be URL-safe.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
27
|
+
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
28
|
+
* expect(decrypted).toBe("Hello, World!")
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @see {@link https://orpc.dev/docs/helpers/encryption | Encryption Helpers}
|
|
32
|
+
*/
|
|
33
|
+
export async function encrypt(value, secret) {
|
|
34
|
+
const encoder = new TextEncoder();
|
|
35
|
+
const data = encoder.encode(value);
|
|
36
|
+
const salt = crypto.getRandomValues(new Uint8Array(CRYPTO_CONSTANTS.SALT_LENGTH));
|
|
37
|
+
const keyMaterial = await crypto.subtle.importKey('raw', encoder.encode(secret), PBKDF2_CONFIG.name, false, ['deriveKey']);
|
|
38
|
+
const key = await crypto.subtle.deriveKey({ ...PBKDF2_CONFIG, salt }, keyMaterial, AES_GCM_CONFIG, false, ['encrypt']);
|
|
39
|
+
const iv = crypto.getRandomValues(new Uint8Array(CRYPTO_CONSTANTS.IV_LENGTH));
|
|
40
|
+
const encrypted = await crypto.subtle.encrypt({ name: AES_GCM_CONFIG.name, iv }, key, data);
|
|
41
|
+
const result = new Uint8Array(salt.length + iv.length + encrypted.byteLength);
|
|
42
|
+
result.set(salt, 0);
|
|
43
|
+
result.set(iv, salt.length);
|
|
44
|
+
result.set(new Uint8Array(encrypted), salt.length + iv.length);
|
|
45
|
+
return encodeBase64url(result);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Decrypts a base64url encoded string using AES-GCM with a secret key.
|
|
49
|
+
* Returns the original string if decryption is successful, or undefined if it fails.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
54
|
+
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
55
|
+
* expect(decrypted).toBe("Hello, World!")
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @see {@link https://orpc.dev/docs/helpers/encryption | Encryption Helpers}
|
|
59
|
+
*/
|
|
60
|
+
export async function decrypt(encrypted, secret) {
|
|
61
|
+
try {
|
|
62
|
+
const data = decodeBase64url(encrypted);
|
|
63
|
+
if (data === undefined) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
const encoder = new TextEncoder();
|
|
67
|
+
const decoder = new TextDecoder();
|
|
68
|
+
const salt = data.slice(0, CRYPTO_CONSTANTS.SALT_LENGTH);
|
|
69
|
+
const iv = data.slice(CRYPTO_CONSTANTS.SALT_LENGTH, CRYPTO_CONSTANTS.SALT_LENGTH + CRYPTO_CONSTANTS.IV_LENGTH);
|
|
70
|
+
const encryptedData = data.slice(CRYPTO_CONSTANTS.SALT_LENGTH + CRYPTO_CONSTANTS.IV_LENGTH);
|
|
71
|
+
const keyMaterial = await crypto.subtle.importKey('raw', encoder.encode(secret), PBKDF2_CONFIG.name, false, ['deriveKey']);
|
|
72
|
+
const key = await crypto.subtle.deriveKey({ ...PBKDF2_CONFIG, salt }, keyMaterial, AES_GCM_CONFIG, false, ['decrypt']);
|
|
73
|
+
const decrypted = await crypto.subtle.decrypt({ name: AES_GCM_CONFIG.name, iv }, key, encryptedData);
|
|
74
|
+
return decoder.decode(decrypted);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// Return undefined if decryption fails (invalid key, corrupted data, etc.)
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
package/dist/helpers/index.d.ts
CHANGED
|
@@ -1,167 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
* Base64url is URL-safe and doesn't use padding
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* const text = "Hello World"
|
|
10
|
-
* const encoded = encodeBase64url(new TextEncoder().encode(text))
|
|
11
|
-
* const decoded = decodeBase64url(encoded)
|
|
12
|
-
* expect(new TextDecoder().decode(decoded)).toEqual(text)
|
|
13
|
-
* ```
|
|
14
|
-
*
|
|
15
|
-
* @see {@link https://orpc.dev/docs/helpers/base64url | Base64Url Helpers}
|
|
16
|
-
*/
|
|
17
|
-
declare function encodeBase64url(data: Uint8Array): string;
|
|
18
|
-
/**
|
|
19
|
-
* Decodes a base64url string to Uint8Array
|
|
20
|
-
* Only accepts canonical, unpadded base64url (the form produced by `encodeBase64url`),
|
|
21
|
-
* returns undefined for anything else, including padded or standard base64 input
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* const text = "Hello World"
|
|
26
|
-
* const encoded = encodeBase64url(new TextEncoder().encode(text))
|
|
27
|
-
* const decoded = decodeBase64url(encoded)
|
|
28
|
-
* expect(new TextDecoder().decode(decoded)).toEqual(text)
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* @see {@link https://orpc.dev/docs/helpers/base64url | Base64Url Helpers}
|
|
32
|
-
*/
|
|
33
|
-
declare function decodeBase64url(base64url: string | undefined | null): Uint8Array<ArrayBuffer> | undefined;
|
|
34
|
-
|
|
35
|
-
interface SetCookieOptions extends Omit<SetCookie, 'name' | 'value'>, StringifyOptions {
|
|
36
|
-
/**
|
|
37
|
-
* Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
|
|
38
|
-
*
|
|
39
|
-
* @default '/'
|
|
40
|
-
*/
|
|
41
|
-
path?: string;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Sets a cookie in the response headers.
|
|
45
|
-
*
|
|
46
|
-
* Does nothing if `headers` is `undefined`.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```ts
|
|
50
|
-
* const headers = new Headers()
|
|
51
|
-
*
|
|
52
|
-
* setCookie(headers, 'sessionId', 'abc123', { httpOnly: true, maxAge: 3600 })
|
|
53
|
-
*
|
|
54
|
-
* expect(headers.get('Set-Cookie')).toBe('sessionId=abc123; Max-Age=3600; Path=/; HttpOnly')
|
|
55
|
-
* ```
|
|
56
|
-
*
|
|
57
|
-
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
58
|
-
*/
|
|
59
|
-
declare function setCookie(headers: Headers | undefined, name: string, value: string, options?: SetCookieOptions): void;
|
|
60
|
-
interface GetCookieOptions extends ParseOptions {
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Gets a cookie value from request headers
|
|
64
|
-
*
|
|
65
|
-
* Returns `undefined` if the cookie is not found or headers are `undefined`.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```ts
|
|
69
|
-
* const headers = new Headers({ 'Cookie': 'sessionId=abc123; theme=dark' })
|
|
70
|
-
*
|
|
71
|
-
* const sessionId = getCookie(headers, 'sessionId')
|
|
72
|
-
*
|
|
73
|
-
* expect(sessionId).toEqual('abc123')
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
77
|
-
*/
|
|
78
|
-
declare function getCookie(headers: Headers | undefined, name: string, options?: GetCookieOptions): string | undefined;
|
|
79
|
-
/**
|
|
80
|
-
* Deletes a cookie by marking it expired.
|
|
81
|
-
*
|
|
82
|
-
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
83
|
-
*/
|
|
84
|
-
declare function deleteCookie(headers: Headers | undefined, name: string, options?: Omit<SetCookieOptions, 'maxAge'>): void;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Encrypts a string using AES-GCM with a secret key.
|
|
88
|
-
* The output is base64url encoded to be URL-safe.
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```ts
|
|
92
|
-
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
93
|
-
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
94
|
-
* expect(decrypted).toBe("Hello, World!")
|
|
95
|
-
* ```
|
|
96
|
-
*
|
|
97
|
-
* @see {@link https://orpc.dev/docs/helpers/encryption | Encryption Helpers}
|
|
98
|
-
*/
|
|
99
|
-
declare function encrypt(value: string, secret: string): Promise<string>;
|
|
100
|
-
/**
|
|
101
|
-
* Decrypts a base64url encoded string using AES-GCM with a secret key.
|
|
102
|
-
* Returns the original string if decryption is successful, or undefined if it fails.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```ts
|
|
106
|
-
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
107
|
-
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
108
|
-
* expect(decrypted).toBe("Hello, World!")
|
|
109
|
-
* ```
|
|
110
|
-
*
|
|
111
|
-
* @see {@link https://orpc.dev/docs/helpers/encryption | Encryption Helpers}
|
|
112
|
-
*/
|
|
113
|
-
declare function decrypt(encrypted: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Signs a string value using HMAC-SHA256 with a secret key.
|
|
117
|
-
*
|
|
118
|
-
* This function creates a cryptographic signature that can be used to verify
|
|
119
|
-
* the integrity and authenticity of the data. The signature is appended to
|
|
120
|
-
* the original value, separated by a dot, using base64url encoding (no padding).
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```ts
|
|
124
|
-
* const signedValue = await sign("user123", "my-secret-key")
|
|
125
|
-
* expect(signedValue).toEqual("user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA")
|
|
126
|
-
* ```
|
|
127
|
-
*
|
|
128
|
-
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
129
|
-
*/
|
|
130
|
-
declare function sign(value: string, secret: string): Promise<string>;
|
|
131
|
-
/**
|
|
132
|
-
* Verifies and extracts the original value from a signed string.
|
|
133
|
-
*
|
|
134
|
-
* This function validates the signature of a previously signed value using the same
|
|
135
|
-
* secret key. If the signature is valid, it returns the original value. If the
|
|
136
|
-
* signature is invalid or the format is incorrect, it returns undefined.
|
|
137
|
-
*
|
|
138
|
-
* @example
|
|
139
|
-
* ```ts
|
|
140
|
-
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
141
|
-
* const originalValue = await unsign(signedValue, "my-secret-key")
|
|
142
|
-
* expect(originalValue).toEqual("user123")
|
|
143
|
-
* ```
|
|
144
|
-
*
|
|
145
|
-
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
146
|
-
*/
|
|
147
|
-
declare function unsign(signedValue: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
148
|
-
/**
|
|
149
|
-
* Extracts the value part from a signed string without verification.
|
|
150
|
-
*
|
|
151
|
-
* This function simply extracts the original value from a signed string
|
|
152
|
-
* without performing any signature verification. It's useful when you need
|
|
153
|
-
* to access the value quickly without the overhead of cryptographic verification.
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* ```ts
|
|
157
|
-
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
158
|
-
* const value = getSignedValue(signedValue)
|
|
159
|
-
* expect(value).toEqual("user123")
|
|
160
|
-
* ```
|
|
161
|
-
*
|
|
162
|
-
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
163
|
-
*/
|
|
164
|
-
declare function getSignedValue(signedValue: string | undefined | null): string | undefined;
|
|
165
|
-
|
|
166
|
-
export { decodeBase64url, decrypt, deleteCookie, encodeBase64url, encrypt, getCookie, getSignedValue, setCookie, sign, unsign };
|
|
167
|
-
export type { GetCookieOptions, SetCookieOptions };
|
|
1
|
+
export * from './base64url.ts';
|
|
2
|
+
export * from './cookie.ts';
|
|
3
|
+
export * from './encryption.ts';
|
|
4
|
+
export * from './signing.ts';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signs a string value using HMAC-SHA256 with a secret key.
|
|
3
|
+
*
|
|
4
|
+
* This function creates a cryptographic signature that can be used to verify
|
|
5
|
+
* the integrity and authenticity of the data. The signature is appended to
|
|
6
|
+
* the original value, separated by a dot, using base64url encoding (no padding).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const signedValue = await sign("user123", "my-secret-key")
|
|
11
|
+
* expect(signedValue).toEqual("user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA")
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
15
|
+
*/
|
|
16
|
+
export declare function sign(value: string, secret: string): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Verifies and extracts the original value from a signed string.
|
|
19
|
+
*
|
|
20
|
+
* This function validates the signature of a previously signed value using the same
|
|
21
|
+
* secret key. If the signature is valid, it returns the original value. If the
|
|
22
|
+
* signature is invalid or the format is incorrect, it returns undefined.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
27
|
+
* const originalValue = await unsign(signedValue, "my-secret-key")
|
|
28
|
+
* expect(originalValue).toEqual("user123")
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
32
|
+
*/
|
|
33
|
+
export declare function unsign(signedValue: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
34
|
+
/**
|
|
35
|
+
* Extracts the value part from a signed string without verification.
|
|
36
|
+
*
|
|
37
|
+
* This function simply extracts the original value from a signed string
|
|
38
|
+
* without performing any signature verification. It's useful when you need
|
|
39
|
+
* to access the value quickly without the overhead of cryptographic verification.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
44
|
+
* const value = getSignedValue(signedValue)
|
|
45
|
+
* expect(value).toEqual("user123")
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
49
|
+
*/
|
|
50
|
+
export declare function getSignedValue(signedValue: string | undefined | null): string | undefined;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { decodeBase64url, encodeBase64url } from './base64url.js';
|
|
2
|
+
const ALGORITHM = { name: 'HMAC', hash: 'SHA-256' };
|
|
3
|
+
/**
|
|
4
|
+
* Signs a string value using HMAC-SHA256 with a secret key.
|
|
5
|
+
*
|
|
6
|
+
* This function creates a cryptographic signature that can be used to verify
|
|
7
|
+
* the integrity and authenticity of the data. The signature is appended to
|
|
8
|
+
* the original value, separated by a dot, using base64url encoding (no padding).
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const signedValue = await sign("user123", "my-secret-key")
|
|
13
|
+
* expect(signedValue).toEqual("user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA")
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
17
|
+
*/
|
|
18
|
+
export async function sign(value, secret) {
|
|
19
|
+
const encoder = new TextEncoder();
|
|
20
|
+
const key = await crypto.subtle.importKey('raw', encoder.encode(secret), ALGORITHM, false, [
|
|
21
|
+
'sign',
|
|
22
|
+
]);
|
|
23
|
+
const signature = await crypto.subtle.sign(ALGORITHM, key, encoder.encode(value));
|
|
24
|
+
return `${value}.${encodeBase64url(new Uint8Array(signature))}`;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Verifies and extracts the original value from a signed string.
|
|
28
|
+
*
|
|
29
|
+
* This function validates the signature of a previously signed value using the same
|
|
30
|
+
* secret key. If the signature is valid, it returns the original value. If the
|
|
31
|
+
* signature is invalid or the format is incorrect, it returns undefined.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
36
|
+
* const originalValue = await unsign(signedValue, "my-secret-key")
|
|
37
|
+
* expect(originalValue).toEqual("user123")
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
41
|
+
*/
|
|
42
|
+
export async function unsign(signedValue, secret) {
|
|
43
|
+
if (typeof signedValue !== 'string') {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
const lastDotIndex = signedValue.lastIndexOf('.');
|
|
47
|
+
if (lastDotIndex === -1) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
const value = signedValue.slice(0, lastDotIndex);
|
|
51
|
+
const signatureBase64url = signedValue.slice(lastDotIndex + 1);
|
|
52
|
+
const signature = decodeBase64url(signatureBase64url);
|
|
53
|
+
if (signature === undefined) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
const encoder = new TextEncoder();
|
|
57
|
+
const key = await crypto.subtle.importKey('raw', encoder.encode(secret), ALGORITHM, false, [
|
|
58
|
+
'verify',
|
|
59
|
+
]);
|
|
60
|
+
const isValid = await crypto.subtle.verify(ALGORITHM, key, signature, encoder.encode(value));
|
|
61
|
+
return isValid ? value : undefined;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Extracts the value part from a signed string without verification.
|
|
65
|
+
*
|
|
66
|
+
* This function simply extracts the original value from a signed string
|
|
67
|
+
* without performing any signature verification. It's useful when you need
|
|
68
|
+
* to access the value quickly without the overhead of cryptographic verification.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
73
|
+
* const value = getSignedValue(signedValue)
|
|
74
|
+
* expect(value).toEqual("user123")
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
78
|
+
*/
|
|
79
|
+
export function getSignedValue(signedValue) {
|
|
80
|
+
if (typeof signedValue !== 'string') {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
const lastDotIndex = signedValue.lastIndexOf('.');
|
|
84
|
+
if (lastDotIndex === -1) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
return signedValue.slice(0, lastDotIndex);
|
|
88
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AnySchema, ErrorMap, InferSchemaInput, InferSchemaOutput, ORPCErrorConstructorMap } from '@orpc/contract';
|
|
2
|
+
import type { IntersectPick } from '@orpc/shared';
|
|
3
|
+
import type { BuilderDefinition } from './builder.ts';
|
|
4
|
+
import type { Context, MergedContext, MergedInitialContext } from './context.ts';
|
|
5
|
+
import type { Middleware } from './middleware.ts';
|
|
6
|
+
import type { ProcedureHandler } from './procedure.ts';
|
|
7
|
+
import { Procedure } from './procedure.ts';
|
|
8
|
+
export declare class ProcedureImplementer<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
|
|
9
|
+
'~orpc': BuilderDefinition<TInputSchema, TOutputSchema, TErrorMap>;
|
|
10
|
+
constructor(definition: BuilderDefinition<TInputSchema, TOutputSchema, TErrorMap>);
|
|
11
|
+
'use'<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $ErrorMap extends ErrorMap = TErrorMap>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>, $ErrorMap>): ProcedureImplementer<MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>, TInputSchema, TOutputSchema, TErrorMap>;
|
|
12
|
+
'handler'(handler: ProcedureHandler<MergedContext<TInitialContext, TInjectedContext>, InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>, ORPCErrorConstructorMap<TErrorMap>>): ImplementedProcedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
|
|
13
|
+
}
|
|
14
|
+
export declare class ImplementedProcedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends Procedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap> {
|
|
15
|
+
use<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $ErrorMap extends ErrorMap = TErrorMap>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>, $ErrorMap>): ImplementedProcedure<MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>, TInputSchema, TOutputSchema, TErrorMap>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Procedure } from './procedure.js';
|
|
2
|
+
export class ProcedureImplementer {
|
|
3
|
+
'~orpc';
|
|
4
|
+
constructor(definition) {
|
|
5
|
+
this['~orpc'] = definition;
|
|
6
|
+
}
|
|
7
|
+
'use'(middleware) {
|
|
8
|
+
return new ProcedureImplementer({
|
|
9
|
+
...this['~orpc'],
|
|
10
|
+
orderedMiddlewares: [
|
|
11
|
+
...this['~orpc'].orderedMiddlewares,
|
|
12
|
+
{
|
|
13
|
+
middleware,
|
|
14
|
+
inputSchemasLengthAtUse: this['~orpc'].inputSchemas?.length,
|
|
15
|
+
outputSchemasLengthAtUse: this['~orpc'].outputSchemas?.length,
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
'handler'(handler) {
|
|
21
|
+
return new ImplementedProcedure({
|
|
22
|
+
...this['~orpc'],
|
|
23
|
+
handler,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class ImplementedProcedure extends Procedure {
|
|
28
|
+
use(middleware) {
|
|
29
|
+
// Since middleware executes before the handler, we use `IntersectPick` to ensure
|
|
30
|
+
// that the middleware's output context ($OutContext) is compatible with the
|
|
31
|
+
// context requirements of the handler, which may have already been defined.
|
|
32
|
+
return new ImplementedProcedure({
|
|
33
|
+
...this['~orpc'],
|
|
34
|
+
orderedMiddlewares: [
|
|
35
|
+
...this['~orpc'].orderedMiddlewares,
|
|
36
|
+
{
|
|
37
|
+
middleware,
|
|
38
|
+
inputSchemasLengthAtUse: this['~orpc'].inputSchemas?.length,
|
|
39
|
+
outputSchemasLengthAtUse: this['~orpc'].outputSchemas?.length,
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ErrorMap, InferRouterContractErrorMap, RouterContract } from '@orpc/contract';
|
|
2
|
+
import { ProcedureContract } from '@orpc/contract';
|
|
3
|
+
import type { IntersectPick, Public } from '@orpc/shared';
|
|
4
|
+
import type { Context, MergedContext, MergedInitialContext } from './context.ts';
|
|
5
|
+
import { ProcedureImplementer } from './implementer-procedure.ts';
|
|
6
|
+
import { Lazy } from './lazy.ts';
|
|
7
|
+
import type { DecoratedMiddleware } from './middleware-decorated.ts';
|
|
8
|
+
import type { AnyMiddleware, Middleware } from './middleware.ts';
|
|
9
|
+
import type { ProcedureConfig } from './procedure.ts';
|
|
10
|
+
import type { AugmentedRouterWithMiddlewares } from './router-utils.ts';
|
|
11
|
+
import type { ContractedRouter } from './router.ts';
|
|
12
|
+
export declare class SharedRouterImplementer<TContract extends RouterContract, TInitialContext extends Context> {
|
|
13
|
+
private readonly contract;
|
|
14
|
+
private readonly config;
|
|
15
|
+
private readonly middlewares;
|
|
16
|
+
private constructor();
|
|
17
|
+
static create<TContract extends RouterContract, TInitialContext extends Context>(contract: TContract, config: ProcedureConfig): SharedRouterImplementer<TContract, TInitialContext>;
|
|
18
|
+
static create<TContract extends RouterContract, TInitialContext extends Context, TInjectedContext extends Context>(contract: TContract, config: ProcedureConfig, middlewares: AnyMiddleware[]): SharedRouterImplementerWithMiddlewares<TContract, TInitialContext, TInjectedContext>;
|
|
19
|
+
use<$OutContext extends IntersectPick<TInitialContext, $OutContext>, $InContext extends Context = TInitialContext, $ErrorMap extends ErrorMap = InferRouterContractErrorMap<TContract>>(middleware: Middleware<$InContext | TInitialContext, $OutContext, unknown, unknown, $ErrorMap>): RouterImplementerWithMiddlewares<TContract, MergedInitialContext<TInitialContext, object, $InContext>, $OutContext>;
|
|
20
|
+
middleware<$OutContext extends IntersectPick<TInitialContext, $OutContext>, $Input, $InContext extends Context = TInitialContext, $Output = any>(middleware: Middleware<$InContext | TInitialContext, $OutContext, $Input, $Output, InferRouterContractErrorMap<TContract>>): DecoratedMiddleware<MergedInitialContext<TInitialContext, object, $InContext>, $OutContext, $Input, $Output, object>;
|
|
21
|
+
router<T extends ContractedRouter<TContract, any>>(router: T): T;
|
|
22
|
+
lazy<T extends ContractedRouter<TContract, any>>(loader: () => Promise<{
|
|
23
|
+
default: T;
|
|
24
|
+
}>): Lazy<T>;
|
|
25
|
+
}
|
|
26
|
+
export interface SharedRouterImplementerWithMiddlewares<TContract extends RouterContract, TInitialContext extends Context, TInjectedContext extends Context> {
|
|
27
|
+
use<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $ErrorMap extends ErrorMap = InferRouterContractErrorMap<TContract>>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, unknown, unknown, $ErrorMap>): RouterImplementerWithMiddlewares<TContract, MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>>;
|
|
28
|
+
middleware<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $Input, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $Output = any>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, $Input, $Output, InferRouterContractErrorMap<TContract>>): DecoratedMiddleware<MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>, $Input, $Output, object>;
|
|
29
|
+
router<T extends ContractedRouter<TContract, MergedContext<TInitialContext, TInjectedContext>>>(router: T): AugmentedRouterWithMiddlewares<T, TInitialContext, TInjectedContext, object>;
|
|
30
|
+
lazy<T extends ContractedRouter<TContract, any>>(loader: () => Promise<{
|
|
31
|
+
default: T;
|
|
32
|
+
}>): Lazy<AugmentedRouterWithMiddlewares<T, TInitialContext, TInjectedContext, object>>;
|
|
33
|
+
}
|
|
34
|
+
export type RouterImplementer<TContract extends RouterContract, TInitialContext extends Context> = TContract extends ProcedureContract<infer $InputSchema, infer $OutputSchema, infer $ErrorMap> ? ProcedureImplementer<TInitialContext, object, $InputSchema, $OutputSchema, $ErrorMap> : Public<SharedRouterImplementer<TContract, TInitialContext>> & {
|
|
35
|
+
[K in keyof TContract]: TContract[K] extends RouterContract ? RouterImplementer<TContract[K], TInitialContext> : never;
|
|
36
|
+
};
|
|
37
|
+
export type RouterImplementerWithMiddlewares<TContract extends RouterContract, TInitialContext extends Context, TInjectedContext extends Context> = TContract extends ProcedureContract<infer $InputSchema, infer $OutputSchema, infer $ErrorMap> ? ProcedureImplementer<TInitialContext, TInjectedContext, $InputSchema, $OutputSchema, $ErrorMap> : SharedRouterImplementerWithMiddlewares<TContract, TInitialContext, TInjectedContext> & {
|
|
38
|
+
[K in keyof TContract]: TContract[K] extends RouterContract ? RouterImplementerWithMiddlewares<TContract[K], TInitialContext, TInjectedContext> : never;
|
|
39
|
+
};
|
|
40
|
+
export declare function createRouterImplementer<TContract extends RouterContract, TInitialContext extends Context>(contract: TContract, config: ProcedureConfig): RouterImplementer<TContract, TInitialContext>;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ProcedureContract } from '@orpc/contract';
|
|
2
|
+
import { bindMethods, omit, setOwn } from '@orpc/shared';
|
|
3
|
+
import { ProcedureImplementer } from './implementer-procedure.js';
|
|
4
|
+
import { Lazy } from './lazy.js';
|
|
5
|
+
import { decorateMiddleware } from './middleware-decorated.js';
|
|
6
|
+
import { Procedure } from './procedure.js';
|
|
7
|
+
import { withHiddenRouterContract } from './router-hidden.js';
|
|
8
|
+
import { augmentImplementedRouter } from './router-utils.js';
|
|
9
|
+
export class SharedRouterImplementer {
|
|
10
|
+
contract;
|
|
11
|
+
config;
|
|
12
|
+
middlewares;
|
|
13
|
+
constructor(contract, config, middlewares) {
|
|
14
|
+
this.contract = contract;
|
|
15
|
+
this.config = config;
|
|
16
|
+
this.middlewares = middlewares;
|
|
17
|
+
}
|
|
18
|
+
static create(contract, config, middlewares = []) {
|
|
19
|
+
return new SharedRouterImplementer(contract, config, middlewares);
|
|
20
|
+
}
|
|
21
|
+
use(middleware) {
|
|
22
|
+
return createRouterImplementerInternal(this.contract, this.config, [
|
|
23
|
+
...this.middlewares,
|
|
24
|
+
middleware,
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
middleware(middleware) {
|
|
28
|
+
const allMiddlewares = [...this.middlewares, middleware];
|
|
29
|
+
let current = decorateMiddleware(allMiddlewares.shift());
|
|
30
|
+
for (const mid of allMiddlewares) {
|
|
31
|
+
current = current.use(mid);
|
|
32
|
+
}
|
|
33
|
+
return current;
|
|
34
|
+
}
|
|
35
|
+
router(router) {
|
|
36
|
+
if (this.middlewares.length) {
|
|
37
|
+
router = augmentImplementedRouter(router, {
|
|
38
|
+
...this.config,
|
|
39
|
+
middlewares: this.middlewares,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return withHiddenRouterContract(router, this.contract);
|
|
43
|
+
}
|
|
44
|
+
lazy(loader) {
|
|
45
|
+
return new Lazy({
|
|
46
|
+
loader: async () => {
|
|
47
|
+
const { default: router } = await loader();
|
|
48
|
+
return { default: this.router(router) };
|
|
49
|
+
},
|
|
50
|
+
meta: {},
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function createRouterImplementer(contract, config) {
|
|
55
|
+
return createRouterImplementerInternal(toPlainRouterContract(contract), config, []);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A normal router can be used as a contract (e.g. for mocking), but its procedures carry a handler,
|
|
59
|
+
* middlewares and config. Kept as is, unimplemented procedures would pass as procedures, their config
|
|
60
|
+
* would override the config passed to `implement`, and handlers serving the hidden contract would run
|
|
61
|
+
* the contract's procedures instead of the actual implementation.
|
|
62
|
+
*
|
|
63
|
+
* Rebuilds the contract with every procedure turned into a plain {@link ProcedureContract}
|
|
64
|
+
* (without `handler`, `orderedMiddlewares` and procedure config), copying only the routers that contain one,
|
|
65
|
+
* so contract-first contracts are returned as is.
|
|
66
|
+
*/
|
|
67
|
+
function toPlainRouterContract(contract) {
|
|
68
|
+
if (contract instanceof ProcedureContract) {
|
|
69
|
+
if (!(contract instanceof Procedure)) {
|
|
70
|
+
return contract;
|
|
71
|
+
}
|
|
72
|
+
// handler & orderedMiddlewares are what make a definition a procedure (see Procedure[Symbol.hasInstance])
|
|
73
|
+
return new ProcedureContract(omit(contract['~orpc'], [
|
|
74
|
+
'handler',
|
|
75
|
+
'orderedMiddlewares',
|
|
76
|
+
'disableInputValidation',
|
|
77
|
+
'disableOutputValidation',
|
|
78
|
+
]));
|
|
79
|
+
}
|
|
80
|
+
let copied;
|
|
81
|
+
for (const [key, child] of Object.entries(contract)) {
|
|
82
|
+
const plainChild = toPlainRouterContract(child);
|
|
83
|
+
if (plainChild !== child) {
|
|
84
|
+
copied ??= { ...contract };
|
|
85
|
+
copied[key] = plainChild;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return (copied ?? contract);
|
|
89
|
+
}
|
|
90
|
+
function createRouterImplementerInternal(contract, config, middlewares) {
|
|
91
|
+
if (contract instanceof ProcedureContract) {
|
|
92
|
+
return new ProcedureImplementer({
|
|
93
|
+
...config,
|
|
94
|
+
...contract['~orpc'],
|
|
95
|
+
orderedMiddlewares: middlewares.map((middleware) => ({ middleware })),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
const implementer = {};
|
|
99
|
+
for (const [key, child] of Object.entries(contract)) {
|
|
100
|
+
setOwn(implementer, key, createRouterImplementerInternal(child, config, middlewares));
|
|
101
|
+
}
|
|
102
|
+
const shared = bindMethods(SharedRouterImplementer.create(contract, config, middlewares));
|
|
103
|
+
for (const [key, method] of Object.entries(shared)) {
|
|
104
|
+
if (Object.hasOwn(implementer, key)) {
|
|
105
|
+
const child = implementer[key];
|
|
106
|
+
implementer[key] = new Proxy(method, {
|
|
107
|
+
get(_, p) {
|
|
108
|
+
return Reflect.get(child, p);
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
implementer[key] = method;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return implementer;
|
|
117
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { RouterContract } from '@orpc/contract';
|
|
2
|
+
import type { DefaultInitialContext } from './builder.ts';
|
|
3
|
+
import type { Context } from './context.ts';
|
|
4
|
+
import type { RouterImplementer } from './implementer-router.ts';
|
|
5
|
+
import type { ProcedureConfig } from './procedure.ts';
|
|
6
|
+
export type Implementer<TContract extends RouterContract, TInitialContext extends Context> = {
|
|
7
|
+
$context<T extends Context = DefaultInitialContext>(): Implementer<TContract, T & object>;
|
|
8
|
+
$config(config: ProcedureConfig): Implementer<TContract, TInitialContext>;
|
|
9
|
+
} & RouterImplementer<TContract, TInitialContext>;
|
|
10
|
+
/**
|
|
11
|
+
* Turns a contract into an implementer, used to implement the contract's
|
|
12
|
+
* procedures, routers, and middleware with full type safety.
|
|
13
|
+
*
|
|
14
|
+
* @see {@link https://orpc.dev/docs/contract/implementation#implementer | Contract Implementation - Implementer}
|
|
15
|
+
*/
|
|
16
|
+
export declare function implement<TContract extends RouterContract, TInitialContext extends Context = DefaultInitialContext>(contract: TContract, config?: ProcedureConfig): Implementer<TContract, TInitialContext & object>;
|