|
1 | | -import { instance, literal, object, pipe, readonly, type BaseIssue, type BaseSchema } from 'valibot'; |
| 1 | +import { |
| 2 | + array, |
| 3 | + instance, |
| 4 | + literal, |
| 5 | + object, |
| 6 | + parse, |
| 7 | + picklist, |
| 8 | + pipe, |
| 9 | + readonly, |
| 10 | + type BaseIssue, |
| 11 | + type BaseSchema, |
| 12 | + type InferOutput |
| 13 | +} from 'valibot'; |
| 14 | + |
| 15 | +// No dangerous value such as: constructor, prototype, etc. |
| 16 | +const allowedSuffixSchema = picklist(['FULFILLED', 'IMPEDED', 'PENDING']); |
| 17 | + |
| 18 | +type AllowedSuffix = InferOutput<typeof allowedSuffixSchema>; |
| 19 | + |
| 20 | +const suffixesSchema = array(allowedSuffixSchema); |
2 | 21 |
|
3 | 22 | export default function createMiddlewareActionSchemas< |
4 | 23 | const TName extends string, |
5 | 24 | const TPayloadSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>>, |
6 | | - const TMetaSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>> |
7 | | ->(actionName: TName, payloadSchema: TPayloadSchema, metaSchema: TMetaSchema) { |
8 | | - return Object.freeze({ |
9 | | - fulfilled: { |
10 | | - name: `${actionName}_FULFILLED` as const, |
11 | | - schema: pipe( |
12 | | - object({ |
13 | | - meta: metaSchema, |
14 | | - payload: payloadSchema, |
15 | | - type: literal(`${actionName}_FULFILLED`) |
16 | | - }), |
17 | | - readonly() |
18 | | - ) |
19 | | - }, |
20 | | - impeded: { |
21 | | - name: `${actionName}_IMPEDED` as const, |
22 | | - schema: pipe( |
23 | | - object({ |
24 | | - meta: metaSchema, |
25 | | - payload: payloadSchema, |
26 | | - type: literal(`${actionName}_IMPEDED`) |
27 | | - }), |
28 | | - readonly() |
29 | | - ) |
30 | | - }, |
31 | | - pending: { |
32 | | - name: `${actionName}_PENDING` as const, |
| 25 | + const TMetaSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>>, |
| 26 | + const TSuffix extends AllowedSuffix |
| 27 | +>(prefix: TName, suffixes: readonly TSuffix[], payloadSchema: TPayloadSchema, metaSchema: TMetaSchema) { |
| 28 | + const result: { |
| 29 | + [K in TSuffix]: Readonly<{ |
| 30 | + name: `${TName}_${K}`; |
| 31 | + schema: BaseSchema< |
| 32 | + unknown, |
| 33 | + { |
| 34 | + meta: InferOutput<typeof metaSchema>; |
| 35 | + payload: InferOutput<typeof payloadSchema>; |
| 36 | + type: `${TName}_${K}`; |
| 37 | + }, |
| 38 | + BaseIssue<unknown> |
| 39 | + >; |
| 40 | + }>; |
| 41 | + } = {} as any; |
| 42 | + |
| 43 | + for (const suffix of parse(suffixesSchema, suffixes)) { |
| 44 | + // We use allowlist to filter the suffix. |
| 45 | + // eslint-disable-next-line security/detect-object-injection |
| 46 | + result[suffix] = { |
| 47 | + name: `${prefix}_${suffix}` as const, |
33 | 48 | schema: pipe( |
34 | 49 | object({ |
35 | 50 | meta: metaSchema, |
36 | 51 | payload: payloadSchema, |
37 | | - type: literal(`${actionName}_PENDING`) |
| 52 | + type: literal(`${prefix}_${suffix}`) |
38 | 53 | }), |
39 | 54 | readonly() |
40 | 55 | ) |
41 | | - }, |
42 | | - rejected: { |
43 | | - name: `${actionName}_REJECTED` as const, |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + return Object.freeze({ |
| 60 | + ...result, |
| 61 | + REJECTED: { |
| 62 | + name: `${prefix}_REJECTED` as const, |
44 | 63 | schema: pipe( |
45 | 64 | object({ |
46 | 65 | error: literal(true), |
47 | 66 | meta: metaSchema, |
48 | 67 | payload: instance(Error), |
49 | | - type: literal(`${actionName}_REJECTED`) |
| 68 | + type: literal(`${prefix}_REJECTED`) |
50 | 69 | }), |
51 | 70 | readonly() |
52 | 71 | ) |
|
0 commit comments