@orpc/shared 0.0.0-next.e9dc36e → 0.0.0-next.eae6003
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 +83 -0
- package/dist/index.d.mts +120 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.mjs +304 -0
- package/package.json +11 -26
- package/dist/error.js +0 -73
- package/dist/error.js.map +0 -1
- package/dist/index.js +0 -81
- package/dist/index.js.map +0 -1
- package/dist/src/error.d.ts +0 -58
- package/dist/src/error.d.ts.map +0 -1
- package/dist/src/index.d.ts +0 -7
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/json.d.ts +0 -2
- package/dist/src/json.d.ts.map +0 -1
- package/dist/src/object.d.ts +0 -8
- package/dist/src/object.d.ts.map +0 -1
- package/dist/src/value.d.ts +0 -4
- package/dist/src/value.d.ts.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/error.test.ts +0 -22
- package/src/error.ts +0 -106
- package/src/index.ts +0 -8
- package/src/json.ts +0 -11
- package/src/object.ts +0 -79
- package/src/value.test-d.ts +0 -22
- package/src/value.test.ts +0 -17
- package/src/value.ts +0 -12
package/src/error.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { ZodError, type ZodIssue } from 'zod'
|
|
2
|
-
|
|
3
|
-
export const ORPC_ERROR_CODE_STATUSES = {
|
|
4
|
-
BAD_REQUEST: 400,
|
|
5
|
-
UNAUTHORIZED: 401,
|
|
6
|
-
FORBIDDEN: 403,
|
|
7
|
-
NOT_FOUND: 404,
|
|
8
|
-
METHOD_NOT_SUPPORTED: 405,
|
|
9
|
-
NOT_ACCEPTABLE: 406,
|
|
10
|
-
TIMEOUT: 408,
|
|
11
|
-
CONFLICT: 409,
|
|
12
|
-
PRECONDITION_FAILED: 412,
|
|
13
|
-
PAYLOAD_TOO_LARGE: 413,
|
|
14
|
-
UNSUPPORTED_MEDIA_TYPE: 415,
|
|
15
|
-
UNPROCESSABLE_CONTENT: 422,
|
|
16
|
-
TOO_MANY_REQUESTS: 429,
|
|
17
|
-
CLIENT_CLOSED_REQUEST: 499,
|
|
18
|
-
|
|
19
|
-
INTERNAL_SERVER_ERROR: 500,
|
|
20
|
-
NOT_IMPLEMENTED: 501,
|
|
21
|
-
BAD_GATEWAY: 502,
|
|
22
|
-
SERVICE_UNAVAILABLE: 503,
|
|
23
|
-
GATEWAY_TIMEOUT: 504,
|
|
24
|
-
} as const
|
|
25
|
-
|
|
26
|
-
export type ORPCErrorCode = keyof typeof ORPC_ERROR_CODE_STATUSES
|
|
27
|
-
|
|
28
|
-
export class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
|
|
29
|
-
constructor(
|
|
30
|
-
public zz$oe: {
|
|
31
|
-
code: TCode
|
|
32
|
-
status?: number
|
|
33
|
-
message?: string
|
|
34
|
-
cause?: unknown
|
|
35
|
-
} & (undefined extends TData ? { data?: TData } : { data: TData }),
|
|
36
|
-
) {
|
|
37
|
-
if (zz$oe.status && (zz$oe.status < 400 || zz$oe.status >= 600)) {
|
|
38
|
-
throw new Error('The ORPCError status code must be in the 400-599 range.')
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
super(zz$oe.message, { cause: zz$oe.cause })
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
get code(): TCode {
|
|
45
|
-
return this.zz$oe.code
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
get status(): number {
|
|
49
|
-
return this.zz$oe.status ?? ORPC_ERROR_CODE_STATUSES[this.code]
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
get data(): TData {
|
|
53
|
-
return this.zz$oe.data as TData
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
get issues(): ZodIssue[] | undefined {
|
|
57
|
-
if (this.code === 'BAD_REQUEST' && this.zz$oe.cause instanceof ZodError) {
|
|
58
|
-
return this.zz$oe.cause.issues
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return undefined
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
toJSON(): {
|
|
65
|
-
code: TCode
|
|
66
|
-
status: number
|
|
67
|
-
message: string
|
|
68
|
-
data: TData
|
|
69
|
-
issues?: ZodIssue[]
|
|
70
|
-
} {
|
|
71
|
-
return {
|
|
72
|
-
code: this.code,
|
|
73
|
-
status: this.status,
|
|
74
|
-
message: this.message,
|
|
75
|
-
data: this.data,
|
|
76
|
-
issues: this.issues,
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
static fromJSON(json: unknown): ORPCError<ORPCErrorCode, any> | undefined {
|
|
81
|
-
if (
|
|
82
|
-
typeof json !== 'object'
|
|
83
|
-
|| json === null
|
|
84
|
-
|| !('code' in json)
|
|
85
|
-
|| !Object.keys(ORPC_ERROR_CODE_STATUSES).find(key => json.code === key)
|
|
86
|
-
|| !('status' in json)
|
|
87
|
-
|| typeof json.status !== 'number'
|
|
88
|
-
|| ('message' in json
|
|
89
|
-
&& json.message !== undefined
|
|
90
|
-
&& typeof json.message !== 'string')
|
|
91
|
-
|| ('issues' in json
|
|
92
|
-
&& json.issues !== undefined
|
|
93
|
-
&& !Array.isArray(json.issues))
|
|
94
|
-
) {
|
|
95
|
-
return undefined
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return new ORPCError({
|
|
99
|
-
code: json.code as ORPCErrorCode,
|
|
100
|
-
status: json.status as number,
|
|
101
|
-
message: Reflect.get(json, 'message') as string,
|
|
102
|
-
data: Reflect.get(json, 'data') as any,
|
|
103
|
-
cause: 'issues' in json ? new ZodError(json.issues as any) : undefined,
|
|
104
|
-
})
|
|
105
|
-
}
|
|
106
|
-
}
|
package/src/index.ts
DELETED
package/src/json.ts
DELETED
package/src/object.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { isPlainObject } from 'is-what'
|
|
2
|
-
|
|
3
|
-
export type Segment = string | number
|
|
4
|
-
|
|
5
|
-
export function set(
|
|
6
|
-
root: Readonly<Record<string, unknown> | unknown[]>,
|
|
7
|
-
segments: Readonly<Segment[]>,
|
|
8
|
-
value: unknown,
|
|
9
|
-
): unknown {
|
|
10
|
-
const ref = { root }
|
|
11
|
-
|
|
12
|
-
let currentRef: any = ref
|
|
13
|
-
let preSegment: string | number = 'root'
|
|
14
|
-
|
|
15
|
-
for (const segment of segments) {
|
|
16
|
-
currentRef = currentRef[preSegment]
|
|
17
|
-
preSegment = segment
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
currentRef[preSegment] = value
|
|
21
|
-
|
|
22
|
-
return ref.root
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function get(
|
|
26
|
-
root: Readonly<Record<string, unknown> | unknown[]>,
|
|
27
|
-
segments: Readonly<Segment[]>,
|
|
28
|
-
): unknown {
|
|
29
|
-
const ref = { root }
|
|
30
|
-
|
|
31
|
-
let currentRef: any = ref
|
|
32
|
-
let preSegment: string | number = 'root'
|
|
33
|
-
|
|
34
|
-
for (const segment of segments) {
|
|
35
|
-
if (
|
|
36
|
-
(typeof currentRef !== 'object' && typeof currentRef !== 'function')
|
|
37
|
-
|| currentRef === null
|
|
38
|
-
) {
|
|
39
|
-
return undefined
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
currentRef = currentRef[preSegment]
|
|
43
|
-
preSegment = segment
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
(typeof currentRef !== 'object' && typeof currentRef !== 'function')
|
|
48
|
-
|| currentRef === null
|
|
49
|
-
) {
|
|
50
|
-
return undefined
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return currentRef[preSegment]
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function findDeepMatches(
|
|
57
|
-
check: (value: unknown) => boolean,
|
|
58
|
-
payload: unknown,
|
|
59
|
-
segments: Segment[] = [],
|
|
60
|
-
maps: Segment[][] = [],
|
|
61
|
-
values: unknown[] = [],
|
|
62
|
-
): { maps: Segment[][], values: unknown[] } {
|
|
63
|
-
if (check(payload)) {
|
|
64
|
-
maps.push(segments)
|
|
65
|
-
values.push(payload)
|
|
66
|
-
}
|
|
67
|
-
else if (Array.isArray(payload)) {
|
|
68
|
-
payload.forEach((v, i) => {
|
|
69
|
-
findDeepMatches(check, v, [...segments, i], maps, values)
|
|
70
|
-
})
|
|
71
|
-
}
|
|
72
|
-
else if (isPlainObject(payload)) {
|
|
73
|
-
for (const key in payload) {
|
|
74
|
-
findDeepMatches(check, payload[key], [...segments, key], maps, values)
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return { maps, values }
|
|
79
|
-
}
|
package/src/value.test-d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { value, type Value } from './value'
|
|
2
|
-
|
|
3
|
-
describe('value', () => {
|
|
4
|
-
it('types', () => {
|
|
5
|
-
let v: Value<number> = 42
|
|
6
|
-
|
|
7
|
-
v = async () => 42
|
|
8
|
-
v = () => 42
|
|
9
|
-
|
|
10
|
-
// @ts-expect-error - not a number
|
|
11
|
-
v = null
|
|
12
|
-
|
|
13
|
-
// @ts-expect-error - not a number
|
|
14
|
-
v = 'string'
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
it('function', () => {
|
|
18
|
-
expectTypeOf(value(Number(42))).toEqualTypeOf<Promise<number>>()
|
|
19
|
-
expectTypeOf(value(() => Number(42))).toEqualTypeOf<Promise<number>>()
|
|
20
|
-
expectTypeOf(value(async () => Number(42))).toEqualTypeOf<Promise<number>>()
|
|
21
|
-
})
|
|
22
|
-
})
|
package/src/value.test.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { value } from './value'
|
|
2
|
-
|
|
3
|
-
it('value', async () => {
|
|
4
|
-
expect(await value(42)).toBe(42)
|
|
5
|
-
expect(await value(() => 42)).toBe(42)
|
|
6
|
-
expect(await value(async () => 42)).toBe(42)
|
|
7
|
-
|
|
8
|
-
expect(await value(() => ({
|
|
9
|
-
then: (resolve: (value: number) => void) => resolve(42),
|
|
10
|
-
}))).toBe(42)
|
|
11
|
-
|
|
12
|
-
expect(await value(async () => ({
|
|
13
|
-
then: (resolve: (value: number) => void) => resolve(42),
|
|
14
|
-
}))).toBe(42)
|
|
15
|
-
|
|
16
|
-
expect(await value(() => ({ value: '42' }))).toEqual({ value: '42' })
|
|
17
|
-
})
|
package/src/value.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Promisable } from 'type-fest'
|
|
2
|
-
|
|
3
|
-
export type Value<T> = T | (() => Promisable<T>)
|
|
4
|
-
|
|
5
|
-
export function value<T extends Value<any>>(value: T):
|
|
6
|
-
Promise<T extends Value<infer U> ? U : never> {
|
|
7
|
-
if (typeof value === 'function') {
|
|
8
|
-
return value()
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
return value as any
|
|
12
|
-
}
|