next-action-plus 1.0.2 → 2.0.1
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 +862 -280
- package/dist/auth/authjs.d.ts +23 -0
- package/dist/auth/authjs.js +18 -0
- package/dist/auth/better-auth.d.ts +34 -0
- package/dist/auth/better-auth.js +17 -0
- package/dist/auth/clerk.d.ts +20 -0
- package/dist/auth/clerk.js +17 -0
- package/dist/auth/iron-session.d.ts +21 -0
- package/dist/auth/iron-session.js +18 -0
- package/dist/auth/kinde.d.ts +25 -0
- package/dist/auth/kinde.js +19 -0
- package/dist/auth/supabase.d.ts +29 -0
- package/dist/auth/supabase.js +17 -0
- package/dist/auth/workos.d.ts +25 -0
- package/dist/auth/workos.js +17 -0
- package/dist/authorize/casl.d.ts +14 -0
- package/dist/authorize/casl.js +10 -0
- package/dist/authorize/cerbos.d.ts +26 -0
- package/dist/authorize/cerbos.js +10 -0
- package/dist/authorize/openfga.d.ts +20 -0
- package/dist/authorize/openfga.js +10 -0
- package/dist/authorize/oso.d.ts +20 -0
- package/dist/authorize/oso.js +10 -0
- package/dist/authorize/permit.d.ts +14 -0
- package/dist/authorize/permit.js +10 -0
- package/dist/authorize.d.ts +11 -0
- package/dist/authorize.js +2 -0
- package/dist/chunk-2INYDUJR.js +16 -0
- package/dist/chunk-A55FM7D6.js +53 -0
- package/dist/chunk-GFGIJNSY.js +19 -0
- package/dist/chunk-IFAKJ72B.js +16 -0
- package/dist/codemod/bin.js +181 -0
- package/dist/concurrency/lock/memory.d.ts +5 -0
- package/dist/concurrency/lock/memory.js +1 -0
- package/dist/concurrency/lock/postgres.d.ts +9 -0
- package/dist/concurrency/lock/postgres.js +45 -0
- package/dist/concurrency/lock/redis.d.ts +9 -0
- package/dist/concurrency/lock/redis.js +30 -0
- package/dist/concurrency.d.ts +21 -0
- package/dist/concurrency.js +48 -0
- package/dist/conformance.d.ts +29 -0
- package/dist/conformance.js +139 -0
- package/dist/gate.d.ts +17 -0
- package/dist/gate.js +2 -0
- package/dist/i18n/next-intl.d.ts +12 -0
- package/dist/i18n/next-intl.js +11 -0
- package/dist/idempotency.d.ts +12 -0
- package/dist/idempotency.js +28 -0
- package/dist/index-BSA9a_EW.d.ts +22 -0
- package/dist/index-C3Ra6AYj.d.ts +37 -0
- package/dist/index-CZFS0TSh.d.ts +53 -0
- package/dist/index-CuAEDv-H.d.ts +64 -0
- package/dist/index-DJ2Ox9am.d.ts +31 -0
- package/dist/index-DV3R2g7H.d.ts +41 -0
- package/dist/index.d.ts +196 -192
- package/dist/index.js +189 -278
- package/dist/kv/memory.d.ts +5 -0
- package/dist/kv/memory.js +27 -0
- package/dist/kv/redis.d.ts +9 -0
- package/dist/kv/redis.js +23 -0
- package/dist/queue/bullmq.d.ts +18 -0
- package/dist/queue/bullmq.js +14 -0
- package/dist/queue/inngest.d.ts +18 -0
- package/dist/queue/inngest.js +14 -0
- package/dist/queue/qstash.d.ts +19 -0
- package/dist/queue/qstash.js +14 -0
- package/dist/queue/trigger.d.ts +16 -0
- package/dist/queue/trigger.js +11 -0
- package/dist/rate/limit/arcjet.d.ts +19 -0
- package/dist/rate/limit/arcjet.js +15 -0
- package/dist/rate/limit/unkey.d.ts +25 -0
- package/dist/rate/limit/unkey.js +14 -0
- package/dist/rate/limit/upstash.d.ts +18 -0
- package/dist/rate/limit/upstash.js +15 -0
- package/dist/rate/limit.d.ts +12 -0
- package/dist/rate/limit.js +17 -0
- package/dist/react.d.ts +40 -0
- package/dist/react.js +110 -0
- package/dist/security/origin.d.ts +11 -0
- package/dist/security/origin.js +21 -0
- package/dist/security/redact.d.ts +9 -0
- package/dist/security/redact.js +15 -0
- package/dist/security/secret.d.ts +6 -0
- package/dist/security/secret.js +13 -0
- package/dist/spec.d.ts +6 -0
- package/dist/spec.js +1 -0
- package/dist/telemetry/otel.d.ts +11 -0
- package/dist/telemetry/otel.js +25 -0
- package/dist/telemetry/pino.d.ts +12 -0
- package/dist/telemetry/pino.js +18 -0
- package/dist/telemetry/sentry.d.ts +16 -0
- package/dist/telemetry/sentry.js +11 -0
- package/package.json +325 -72
- package/dist/index.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,291 +1,202 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
import { errorCreate, errorGet } from './chunk-IFAKJ72B.js';
|
|
2
|
+
export { errorCreate, errorGet } from './chunk-IFAKJ72B.js';
|
|
3
|
+
|
|
4
|
+
// src/form/decode.ts
|
|
5
|
+
var formDecode = (props) => {
|
|
6
|
+
const { form } = props;
|
|
7
|
+
const root = {};
|
|
8
|
+
for (const [name, value] of form.entries()) {
|
|
9
|
+
if (name.startsWith("$ACTION")) continue;
|
|
10
|
+
if (typeof value !== "string" && value.size === 0 && value.name === "") continue;
|
|
11
|
+
const segments = name.match(/[^.[\]]+|\[\]/g) ?? [name];
|
|
12
|
+
if (segments.some((segment) => segment === "__proto__" || segment === "constructor" || segment === "prototype")) throw errorCreate({ code: "LIMIT", phase: "limit", message: `Input (${name}) is error: forbidden key` });
|
|
13
|
+
let parent = root;
|
|
14
|
+
segments.forEach((segment, index) => {
|
|
15
|
+
const isLast = index === segments.length - 1;
|
|
16
|
+
const key = segment === "[]" ? String(parent.length) : segment;
|
|
17
|
+
const holder = parent;
|
|
18
|
+
if (isLast) {
|
|
19
|
+
const existing = holder[key];
|
|
20
|
+
holder[key] = existing === void 0 ? value : Array.isArray(existing) ? [...existing, value] : [existing, value];
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const following = segments[index + 1];
|
|
24
|
+
if (typeof holder[key] !== "object") holder[key] = following === "[]" || /^\d+$/.test(following) ? [] : {};
|
|
25
|
+
parent = holder[key];
|
|
26
|
+
});
|
|
21
27
|
}
|
|
22
|
-
|
|
23
|
-
var isActionPlusError = (error) => {
|
|
24
|
-
if (typeof error !== "object" || error === null) return false;
|
|
25
|
-
if (!("code" in error)) return false;
|
|
26
|
-
const name = error.name;
|
|
27
|
-
return name === "ActionPlusError" || name === "ActionPlusValidationError";
|
|
28
|
-
};
|
|
29
|
-
var isActionPlusValidationError = (error) => {
|
|
30
|
-
return typeof error === "object" && error !== null && "issues" in error && error.name === "ActionPlusValidationError";
|
|
28
|
+
return root;
|
|
31
29
|
};
|
|
32
30
|
|
|
33
|
-
// src/
|
|
34
|
-
var
|
|
31
|
+
// src/schema/parse.ts
|
|
32
|
+
var schemaParse = async (props) => {
|
|
33
|
+
const { schema, value, path } = props;
|
|
34
|
+
const standard = schema?.["~standard"];
|
|
35
|
+
if (!standard || typeof standard.validate !== "function") throw new TypeError("next-action-plus: unsupported schema. Use a Standard Schema validator (Zod 3.24+, Valibot, ArkType, Effect Schema, ...) or an object with a `~standard` property.");
|
|
36
|
+
const result = await standard.validate(value);
|
|
37
|
+
if (!result.issues) return result.value;
|
|
38
|
+
const issues = result.issues.map((issue) => ({
|
|
39
|
+
path: [...path ?? [], ...(issue.path ?? []).map((segment) => {
|
|
40
|
+
const key = typeof segment === "object" && segment !== null ? segment.key : segment;
|
|
41
|
+
return typeof key === "number" ? key : String(key);
|
|
42
|
+
})],
|
|
43
|
+
message: issue.message
|
|
44
|
+
}));
|
|
45
|
+
const first = issues[0];
|
|
46
|
+
throw errorCreate({ code: "VALIDATION", phase: "validation", issues, message: first ? `Input (${first.path.length ? first.path.join(".") : "unknown"}) is error: ${first.message}` : "Input is error" });
|
|
47
|
+
};
|
|
35
48
|
|
|
36
|
-
// src/
|
|
37
|
-
var
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Adds a middleware function
|
|
62
|
-
* @param middleware Middleware function
|
|
63
|
-
*/
|
|
64
|
-
use(middleware) {
|
|
65
|
-
return new _ActionPlus({
|
|
66
|
-
schemas: this.schemas,
|
|
67
|
-
middlewares: [...this.middlewares, middleware],
|
|
68
|
-
ctx: this.ctx,
|
|
69
|
-
options: this.options
|
|
70
|
-
});
|
|
49
|
+
// src/security/limit.ts
|
|
50
|
+
var limitCheck = (props) => {
|
|
51
|
+
const { value, limit } = props;
|
|
52
|
+
const max = { depth: limit.depth ?? 32, keys: limit.keys ?? 1e4, string: limit.string ?? 1e6, array: limit.array ?? 1e4, file: limit.file?.size ?? 10 * 1024 * 1024 };
|
|
53
|
+
const fail = (reason) => errorCreate({ code: "LIMIT", phase: "limit", message: `Input is error: ${reason}` });
|
|
54
|
+
let keys = 0;
|
|
55
|
+
const stack = [{ item: value, depth: 0 }];
|
|
56
|
+
while (stack.length) {
|
|
57
|
+
const { item, depth } = stack.pop();
|
|
58
|
+
if (typeof item === "string" && item.length > max.string) throw fail(`string longer than ${max.string}`);
|
|
59
|
+
if (typeof Blob !== "undefined" && item instanceof Blob) {
|
|
60
|
+
if (item.size > max.file) throw fail(`file larger than ${max.file} bytes`);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (typeof item !== "object" || item === null || item instanceof Date) continue;
|
|
64
|
+
if (depth >= max.depth) throw fail(`nested deeper than ${max.depth}`);
|
|
65
|
+
const entries = Array.isArray(item) ? item : Object.keys(item);
|
|
66
|
+
if (Array.isArray(item) && item.length > max.array) throw fail(`array longer than ${max.array}`);
|
|
67
|
+
if (!Array.isArray(item) && ["__proto__", "constructor", "prototype"].some((key) => Object.prototype.hasOwnProperty.call(item, key))) throw fail("forbidden key");
|
|
68
|
+
keys += entries.length;
|
|
69
|
+
if (keys > max.keys) throw fail(`more than ${max.keys} keys`);
|
|
70
|
+
for (const child of Array.isArray(item) ? item : Object.values(item)) stack.push({ item: child, depth: depth + 1 });
|
|
71
71
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/action/run.ts
|
|
75
|
+
var navigationIs = (props) => {
|
|
76
|
+
const digest = props.error?.digest;
|
|
77
|
+
return typeof digest === "string" && (digest.startsWith("NEXT_") || digest === "DYNAMIC_SERVER_USAGE" || digest === "BAILOUT_TO_CLIENT_SIDE_RENDERING");
|
|
78
|
+
};
|
|
79
|
+
var actionRun = async (props) => {
|
|
80
|
+
const { state, handler, params, previous } = props;
|
|
81
|
+
const { options } = state;
|
|
82
|
+
const correlationId = crypto.randomUUID();
|
|
83
|
+
const response = state.response ?? options.response;
|
|
84
|
+
const errors = Object.fromEntries(Object.entries(state.errors).map(([code, definition]) => [code, (factory) => errorCreate({ code, status: definition.status, message: factory?.message ?? definition.message ?? code, data: factory?.data, cause: factory?.cause, expose: true })]));
|
|
85
|
+
const failure = (failureProps) => {
|
|
86
|
+
const found = failureProps.phase === "output" ? void 0 : errorGet({ error: failureProps.error });
|
|
87
|
+
const error2 = found ? Object.assign(found, { phase: found.phase ?? failureProps.phase, correlationId }) : errorCreate({ code: "INTERNAL", message: "Internal error", phase: failureProps.phase, cause: failureProps.error, correlationId });
|
|
88
|
+
return { "~result": true, ok: false, error: error2, input: failureProps.input };
|
|
89
|
+
};
|
|
90
|
+
let result;
|
|
91
|
+
let input = params[state.args.length];
|
|
92
|
+
let phase = "limit";
|
|
93
|
+
try {
|
|
94
|
+
if (typeof FormData !== "undefined" && input instanceof FormData) input = formDecode({ form: input });
|
|
95
|
+
if (options.limit !== false) limitCheck({ value: [...params.slice(0, state.args.length), input], limit: options.limit ?? {} });
|
|
96
|
+
phase = "validation";
|
|
97
|
+
const args = await Promise.all(state.args.map((schema, index) => schemaParse({ schema, value: params[index], path: ["args", index] })));
|
|
98
|
+
const step = async (stepProps) => {
|
|
99
|
+
const current = state.steps[stepProps.index];
|
|
100
|
+
let stepPhase = current ? current.kind === "input" ? "validation" : "middleware" : "handler";
|
|
81
101
|
try {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const baseContext = {
|
|
97
|
-
phase,
|
|
98
|
-
error,
|
|
99
|
-
input: includeInput ? input : void 0,
|
|
100
|
-
parsedInput: includeInput ? parsedInputForErrors : void 0,
|
|
101
|
-
ctx: includeInput ? ctxForErrors : void 0
|
|
102
|
-
};
|
|
103
|
-
if (this.isValidationError(error)) {
|
|
104
|
-
const { message, issues } = this.formatValidationErrorParts(error);
|
|
105
|
-
const validationContext = { ...baseContext, message, issues };
|
|
106
|
-
this.options.onError?.(validationContext);
|
|
107
|
-
const formatted = this.options.formatValidationError?.(validationContext) ?? new ActionPlusValidationError(message, {
|
|
108
|
-
issues,
|
|
109
|
-
phase,
|
|
110
|
-
cause: error,
|
|
111
|
-
data: includeInput ? { input, parsedInput: parsedInputForErrors, ctx: ctxForErrors } : void 0
|
|
102
|
+
if (current?.kind === "input") return await step({ index: stepProps.index + 1, ctx: stepProps.ctx, input: await schemaParse({ schema: current.schema, value: stepProps.input }) });
|
|
103
|
+
if (current?.kind === "use") {
|
|
104
|
+
let isCalled = false;
|
|
105
|
+
const outcome = await current.plugin.run({
|
|
106
|
+
input: stepProps.input,
|
|
107
|
+
args,
|
|
108
|
+
ctx: Object.freeze({ ...stepProps.ctx }),
|
|
109
|
+
meta: state.meta,
|
|
110
|
+
errors,
|
|
111
|
+
next: (async (nextProps) => {
|
|
112
|
+
if (isCalled) throw errorCreate({ code: "INTERNAL", message: `Middleware "${current.plugin.name}" called next() twice` });
|
|
113
|
+
isCalled = true;
|
|
114
|
+
return step({ index: stepProps.index + 1, ctx: { ...stepProps.ctx, ...nextProps?.ctx }, input: stepProps.input });
|
|
115
|
+
})
|
|
112
116
|
});
|
|
113
|
-
throw
|
|
117
|
+
if (outcome?.["~result"] !== true) throw new TypeError(`next-action-plus: middleware "${current.plugin.name}" must return the result of next()`);
|
|
118
|
+
return outcome;
|
|
114
119
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
120
|
+
const data = await handler({ input: stepProps.input, args, ctx: Object.freeze({ ...stepProps.ctx }), meta: state.meta, errors, ...previous && { state: { previous: previous.value } } });
|
|
121
|
+
stepPhase = "output";
|
|
122
|
+
return { "~result": true, ok: true, data: state.output ? await schemaParse({ schema: state.output, value: data }) : data, input: stepProps.input };
|
|
123
|
+
} catch (error2) {
|
|
124
|
+
if (navigationIs({ error: error2 })) throw error2;
|
|
125
|
+
return failure({ error: error2, phase: stepPhase, input: stepProps.input });
|
|
119
126
|
}
|
|
120
127
|
};
|
|
128
|
+
result = await step({ index: 0, ctx: {}, input });
|
|
129
|
+
} catch (error2) {
|
|
130
|
+
if (navigationIs({ error: error2 })) throw error2;
|
|
131
|
+
result = failure({ error: error2, phase, input });
|
|
121
132
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (typeof key === "string" || typeof key === "number") return key;
|
|
129
|
-
return String(key);
|
|
130
|
-
}
|
|
131
|
-
return String(seg);
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
normalizeIssues(error) {
|
|
135
|
-
if (typeof error !== "object" || error === null) return [];
|
|
136
|
-
const maybe = error;
|
|
137
|
-
const errorsCandidate = maybe.errors;
|
|
138
|
-
const issuesCandidate = maybe.issues;
|
|
139
|
-
const rawIssues = Array.isArray(errorsCandidate) ? errorsCandidate : Array.isArray(issuesCandidate) ? issuesCandidate : [];
|
|
140
|
-
return rawIssues.filter((issue) => typeof issue === "object" && issue !== null).map((issue) => ({
|
|
141
|
-
path: this.normalizeIssuePath(issue.path),
|
|
142
|
-
message: typeof issue.message === "string" ? issue.message : String(issue.message ?? ""),
|
|
143
|
-
raw: issue
|
|
144
|
-
}));
|
|
145
|
-
}
|
|
146
|
-
formatValidationErrorParts(error) {
|
|
147
|
-
const issues = this.normalizeIssues(error);
|
|
148
|
-
const first = issues[0];
|
|
149
|
-
if (!first) {
|
|
150
|
-
return { message: `Validation error: ${String(error)}`, issues: [] };
|
|
151
|
-
}
|
|
152
|
-
const fieldPath = first.path.length ? first.path.join(".") : "unknown";
|
|
153
|
-
const message = first.message || String(error);
|
|
154
|
-
return { message: `Input (${fieldPath}) is error: ${message}`, issues };
|
|
155
|
-
}
|
|
156
|
-
async validateInput(input) {
|
|
157
|
-
if (!this.schemas.length) return input;
|
|
158
|
-
const schemasToRun = [...this.schemas];
|
|
159
|
-
let baseInput = input;
|
|
160
|
-
if (input instanceof FormData) {
|
|
161
|
-
for (let i = 0; i < schemasToRun.length; i++) {
|
|
162
|
-
const candidate = schemasToRun[i];
|
|
163
|
-
const parsed = await this.tryParseSchema(candidate, input);
|
|
164
|
-
if (!parsed.ok) continue;
|
|
165
|
-
schemasToRun.splice(i, 1);
|
|
166
|
-
baseInput = parsed.value;
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (schemasToRun.length === 0) {
|
|
171
|
-
return baseInput;
|
|
172
|
-
}
|
|
173
|
-
if (schemasToRun.length === 1) {
|
|
174
|
-
return await this.parseSchema(schemasToRun[0], baseInput);
|
|
175
|
-
}
|
|
176
|
-
let mergedObject;
|
|
177
|
-
let lastNonObject = void 0;
|
|
178
|
-
for (const schema of schemasToRun) {
|
|
179
|
-
const parsed = await this.parseSchema(schema, baseInput);
|
|
180
|
-
if (this.isPlainObject(parsed)) {
|
|
181
|
-
mergedObject = { ...mergedObject ?? {}, ...parsed };
|
|
182
|
-
} else {
|
|
183
|
-
lastNonObject = parsed;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
return mergedObject ?? lastNonObject;
|
|
187
|
-
}
|
|
188
|
-
isPlainObject(value) {
|
|
189
|
-
return typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof Date);
|
|
190
|
-
}
|
|
191
|
-
async parseSchema(schema, input) {
|
|
192
|
-
if (schema && typeof schema === "object") {
|
|
193
|
-
const maybeAsync = schema.parseAsync;
|
|
194
|
-
if (typeof maybeAsync === "function") {
|
|
195
|
-
return await schema.parseAsync(input);
|
|
196
|
-
}
|
|
197
|
-
const maybeSync = schema.parse;
|
|
198
|
-
if (typeof maybeSync === "function") {
|
|
199
|
-
return schema.parse(input);
|
|
200
|
-
}
|
|
201
|
-
const maybeStandard = schema["~standard"];
|
|
202
|
-
if (maybeStandard && typeof maybeStandard === "object") {
|
|
203
|
-
const validate = maybeStandard.validate;
|
|
204
|
-
if (typeof validate === "function") {
|
|
205
|
-
const result = await validate(input);
|
|
206
|
-
if (result && typeof result === "object" && "issues" in result) {
|
|
207
|
-
const err = new Error("Validation error");
|
|
208
|
-
err.issues = result.issues;
|
|
209
|
-
throw err;
|
|
210
|
-
}
|
|
211
|
-
if (result && typeof result === "object" && "value" in result) {
|
|
212
|
-
return result.value;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
throw new Error("Unsupported schema type");
|
|
218
|
-
}
|
|
219
|
-
async tryParseSchema(schema, input) {
|
|
220
|
-
try {
|
|
221
|
-
if (schema && typeof schema === "object") {
|
|
222
|
-
const safeAsync = schema.safeParseAsync;
|
|
223
|
-
if (typeof safeAsync === "function") {
|
|
224
|
-
const res = await schema.safeParseAsync(input);
|
|
225
|
-
return res.success ? { ok: true, value: res.data } : { ok: false };
|
|
226
|
-
}
|
|
227
|
-
const safeSync = schema.safeParse;
|
|
228
|
-
if (typeof safeSync === "function") {
|
|
229
|
-
const res = schema.safeParse(input);
|
|
230
|
-
return res.success ? { ok: true, value: res.data } : { ok: false };
|
|
231
|
-
}
|
|
232
|
-
const maybeStandard = schema["~standard"];
|
|
233
|
-
if (maybeStandard && typeof maybeStandard === "object") {
|
|
234
|
-
const validate = maybeStandard.validate;
|
|
235
|
-
if (typeof validate === "function") {
|
|
236
|
-
const result = await validate(input);
|
|
237
|
-
if (result && typeof result === "object" && "issues" in result) return { ok: false };
|
|
238
|
-
if (result && typeof result === "object" && "value" in result) return { ok: true, value: result.value };
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
return { ok: true, value: await this.parseSchema(schema, input) };
|
|
243
|
-
} catch {
|
|
244
|
-
return { ok: false };
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
async runMiddlewareChain(input) {
|
|
248
|
-
let ctx = { ...this.ctx };
|
|
249
|
-
let currentIndex = 0;
|
|
250
|
-
const middlewares = this.middlewares;
|
|
251
|
-
const runNext = async (options) => {
|
|
252
|
-
if (options?.ctx) {
|
|
253
|
-
ctx = { ...ctx, ...options.ctx };
|
|
254
|
-
}
|
|
255
|
-
if (currentIndex < middlewares.length) {
|
|
256
|
-
const middleware = middlewares[currentIndex];
|
|
257
|
-
currentIndex++;
|
|
258
|
-
return await middleware({
|
|
259
|
-
input,
|
|
260
|
-
ctx: Object.freeze({ ...ctx }),
|
|
261
|
-
next: runNext
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
return ctx;
|
|
265
|
-
};
|
|
266
|
-
return await runNext();
|
|
267
|
-
}
|
|
268
|
-
isValidationError(error) {
|
|
269
|
-
if (typeof error !== "object" || error === null) return false;
|
|
270
|
-
if ("name" in error) {
|
|
271
|
-
const errName = error.name;
|
|
272
|
-
if (errName === "ZodError" || errName.includes("ValidationError")) {
|
|
273
|
-
return true;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
return "errors" in error || "issues" in error;
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
var createActionPlus = (options) => {
|
|
280
|
-
return new ActionPlus({ options });
|
|
133
|
+
if (result.ok) return response ? response.success({ data: result.data, info: { meta: state.meta, correlationId } }) : result.data;
|
|
134
|
+
const error = result.error;
|
|
135
|
+
if (options.log !== false) (options.log ?? console).error("Action error:", error);
|
|
136
|
+
if (options.error?.message && error.expose) error.message = await options.error.message({ error });
|
|
137
|
+
if (response) return response.failure({ error, info: { meta: state.meta, correlationId } });
|
|
138
|
+
throw error.code === "INTERNAL" && error.cause !== void 0 && !errorGet({ error: error.cause }) && error.phase !== "output" ? error.cause : error;
|
|
281
139
|
};
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
140
|
+
|
|
141
|
+
// src/action/create.ts
|
|
142
|
+
var actionBuilder = (props) => {
|
|
143
|
+
const { state } = props;
|
|
144
|
+
const change = (next) => actionBuilder({ state: { ...state, ...next } });
|
|
145
|
+
const gateCheck = () => {
|
|
146
|
+
if (state.gate.required && !state.gate.satisfied) throw new Error("next-action-plus: this action has no gate. Add an auth plugin with .use(...), call .public(), or create the client with `gate: { required: false }`.");
|
|
147
|
+
};
|
|
148
|
+
return {
|
|
149
|
+
input: (schema) => {
|
|
150
|
+
if (state.steps.some((step) => step.kind === "input")) throw new Error("next-action-plus: .input() is already set for this action");
|
|
151
|
+
return change({ steps: [...state.steps, { kind: "input", schema }] });
|
|
152
|
+
},
|
|
153
|
+
args: (schemas) => change({ args: schemas }),
|
|
154
|
+
output: (schema) => change({ output: schema }),
|
|
155
|
+
meta: (meta) => change({ meta: { ...state.meta, ...meta } }),
|
|
156
|
+
errors: (errors) => change({ errors: { ...state.errors, ...errors } }),
|
|
157
|
+
use: (middleware) => {
|
|
158
|
+
const plugin = typeof middleware === "function" ? { name: middleware.name || "middleware", run: middleware } : middleware;
|
|
159
|
+
return change({ steps: [...state.steps, { kind: "use", plugin }], errors: { ...state.errors, ...plugin.errors }, gate: { ...state.gate, satisfied: state.gate.satisfied || plugin.gate === true } });
|
|
160
|
+
},
|
|
161
|
+
response: (response) => change({ response }),
|
|
162
|
+
public: () => change({ gate: { ...state.gate, satisfied: true } }),
|
|
163
|
+
action: (handler) => {
|
|
164
|
+
gateCheck();
|
|
165
|
+
return async (...params) => actionRun({ state, handler, params });
|
|
166
|
+
},
|
|
167
|
+
actionState: (handler) => {
|
|
168
|
+
gateCheck();
|
|
169
|
+
return async (...params) => actionRun({ state, handler, params: params.filter((_, index) => index !== state.args.length), previous: { value: params[state.args.length] } });
|
|
170
|
+
}
|
|
171
|
+
};
|
|
290
172
|
};
|
|
291
|
-
|
|
173
|
+
var actionCreate = (props) => actionBuilder({ state: { options: props ?? {}, steps: [], args: [], output: void 0, meta: {}, errors: {}, response: void 0, gate: { required: props?.gate?.required !== false, satisfied: false } } });
|
|
174
|
+
|
|
175
|
+
// src/response/define.ts
|
|
176
|
+
var responseDefine = (props) => props;
|
|
177
|
+
|
|
178
|
+
// src/response/tuple.ts
|
|
179
|
+
var responseTuple = () => ({
|
|
180
|
+
success: (props) => [true, props.data],
|
|
181
|
+
failure: (props) => [false, props.error.message, { code: props.error.code, issues: props.error.issues, data: props.error.data, correlationId: props.error.correlationId }],
|
|
182
|
+
ok: (props) => Array.isArray(props.result) && props.result[0] === true
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// src/response/result.ts
|
|
186
|
+
var responseResult = () => ({
|
|
187
|
+
success: (props) => ({ ok: true, data: props.data }),
|
|
188
|
+
failure: (props) => ({ ok: false, error: { code: props.error.code, message: props.error.message, issues: props.error.issues, data: props.error.data, correlationId: props.error.correlationId } }),
|
|
189
|
+
ok: (props) => props.result?.ok === true
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// src/response/problem.ts
|
|
193
|
+
var responseProblem = () => ({
|
|
194
|
+
success: (props) => props.data,
|
|
195
|
+
failure: (props) => ({ type: "about:blank", title: props.error.code, status: props.error.status, detail: props.error.message, code: props.error.code, issues: props.error.issues, data: props.error.data, correlationId: props.error.correlationId }),
|
|
196
|
+
ok: (props) => {
|
|
197
|
+
const result = props.result;
|
|
198
|
+
return !(result?.type === "about:blank" && typeof result.title === "string" && typeof result.status === "number");
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
export { actionCreate, formDecode, responseDefine, responseProblem, responseResult, responseTuple, schemaParse };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/kv/memory.ts
|
|
2
|
+
var kvMemory = () => {
|
|
3
|
+
const store = /* @__PURE__ */ new Map();
|
|
4
|
+
const read = (key) => {
|
|
5
|
+
const entry = store.get(key);
|
|
6
|
+
if (entry && entry.expiresAt <= Date.now()) store.delete(key);
|
|
7
|
+
return store.get(key);
|
|
8
|
+
};
|
|
9
|
+
const write = (props) => void store.set(props.key, { value: JSON.stringify(props.value), expiresAt: props.ttlMs ? Date.now() + props.ttlMs : Number.POSITIVE_INFINITY });
|
|
10
|
+
return {
|
|
11
|
+
name: "memory",
|
|
12
|
+
version: 1,
|
|
13
|
+
get: async (props) => {
|
|
14
|
+
const entry = read(props.key);
|
|
15
|
+
return entry ? JSON.parse(entry.value) : void 0;
|
|
16
|
+
},
|
|
17
|
+
set: async (props) => write(props),
|
|
18
|
+
add: async (props) => {
|
|
19
|
+
if (read(props.key)) return false;
|
|
20
|
+
write(props);
|
|
21
|
+
return true;
|
|
22
|
+
},
|
|
23
|
+
delete: async (props) => void store.delete(props.key)
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export { kvMemory };
|
package/dist/kv/redis.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/kv/redis.ts
|
|
2
|
+
var kvRedis = (props) => {
|
|
3
|
+
const { client } = props;
|
|
4
|
+
const prefix = props.prefix ?? "nap:kv:";
|
|
5
|
+
const write = async (writeProps, isAdd) => await client.eval({
|
|
6
|
+
script: "local a = {'SET', KEYS[1], ARGV[1]} if tonumber(ARGV[2]) > 0 then table.insert(a, 'PX') table.insert(a, ARGV[2]) end if ARGV[3] == '1' then table.insert(a, 'NX') end if redis.call(unpack(a)) then return 1 end return 0",
|
|
7
|
+
keys: [`${prefix}${writeProps.key}`],
|
|
8
|
+
args: [JSON.stringify(writeProps.value), writeProps.ttlMs ?? 0, isAdd ? "1" : "0"]
|
|
9
|
+
}) === 1;
|
|
10
|
+
return {
|
|
11
|
+
name: "redis",
|
|
12
|
+
version: 1,
|
|
13
|
+
get: async (getProps) => {
|
|
14
|
+
const raw = await client.eval({ script: "return redis.call('GET', KEYS[1])", keys: [`${prefix}${getProps.key}`], args: [] });
|
|
15
|
+
return typeof raw === "string" ? JSON.parse(raw) : void 0;
|
|
16
|
+
},
|
|
17
|
+
set: async (setProps) => void await write(setProps, false),
|
|
18
|
+
add: async (addProps) => write(addProps, true),
|
|
19
|
+
delete: async (deleteProps) => void await client.eval({ script: "return redis.call('DEL', KEYS[1])", keys: [`${prefix}${deleteProps.key}`], args: [] })
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { kvRedis };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { d as TQueue } from '../index-CZFS0TSh.js';
|
|
2
|
+
import '../index-DV3R2g7H.js';
|
|
3
|
+
import '../index-CuAEDv-H.js';
|
|
4
|
+
|
|
5
|
+
declare const queueBullmq: (props: Readonly<{
|
|
6
|
+
queue: {
|
|
7
|
+
add: (name: string, data: unknown, options?: {
|
|
8
|
+
deduplication?: {
|
|
9
|
+
id: string;
|
|
10
|
+
};
|
|
11
|
+
delay?: number;
|
|
12
|
+
}) => Promise<{
|
|
13
|
+
id?: string;
|
|
14
|
+
}>;
|
|
15
|
+
};
|
|
16
|
+
}>) => TQueue;
|
|
17
|
+
|
|
18
|
+
export { queueBullmq };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/queue/bullmq.ts
|
|
2
|
+
var queueBullmq = (props) => {
|
|
3
|
+
const { queue } = props;
|
|
4
|
+
return {
|
|
5
|
+
name: "bullmq",
|
|
6
|
+
version: 1,
|
|
7
|
+
enqueue: async (enqueueProps) => {
|
|
8
|
+
const job = await queue.add(enqueueProps.name, enqueueProps.payload, { ...enqueueProps.dedupe && { deduplication: { id: enqueueProps.dedupe.id } }, ...enqueueProps.delayMs !== void 0 && { delay: enqueueProps.delayMs } });
|
|
9
|
+
return { id: job.id ?? "" };
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { queueBullmq };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { d as TQueue } from '../index-CZFS0TSh.js';
|
|
2
|
+
import '../index-DV3R2g7H.js';
|
|
3
|
+
import '../index-CuAEDv-H.js';
|
|
4
|
+
|
|
5
|
+
declare const queueInngest: (props: Readonly<{
|
|
6
|
+
inngest: {
|
|
7
|
+
send: (event: {
|
|
8
|
+
name: string;
|
|
9
|
+
data: unknown;
|
|
10
|
+
id?: string;
|
|
11
|
+
ts?: number;
|
|
12
|
+
}) => Promise<{
|
|
13
|
+
ids: string[];
|
|
14
|
+
}>;
|
|
15
|
+
};
|
|
16
|
+
}>) => TQueue;
|
|
17
|
+
|
|
18
|
+
export { queueInngest };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/queue/inngest.ts
|
|
2
|
+
var queueInngest = (props) => {
|
|
3
|
+
const { inngest } = props;
|
|
4
|
+
return {
|
|
5
|
+
name: "inngest",
|
|
6
|
+
version: 1,
|
|
7
|
+
enqueue: async (enqueueProps) => {
|
|
8
|
+
const sent = await inngest.send({ name: enqueueProps.name, data: enqueueProps.payload, ...enqueueProps.dedupe && { id: enqueueProps.dedupe.id }, ...enqueueProps.delayMs !== void 0 && { ts: Date.now() + enqueueProps.delayMs } });
|
|
9
|
+
return { id: sent.ids[0] ?? "" };
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { queueInngest };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { d as TQueue } from '../index-CZFS0TSh.js';
|
|
2
|
+
import '../index-DV3R2g7H.js';
|
|
3
|
+
import '../index-CuAEDv-H.js';
|
|
4
|
+
|
|
5
|
+
declare const queueQstash: (props: Readonly<{
|
|
6
|
+
client: {
|
|
7
|
+
publishJSON: (props: {
|
|
8
|
+
url: string;
|
|
9
|
+
body: unknown;
|
|
10
|
+
deduplicationId?: string;
|
|
11
|
+
delay?: number;
|
|
12
|
+
}) => Promise<{
|
|
13
|
+
messageId: string;
|
|
14
|
+
}>;
|
|
15
|
+
};
|
|
16
|
+
url: string;
|
|
17
|
+
}>) => TQueue;
|
|
18
|
+
|
|
19
|
+
export { queueQstash };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/queue/qstash.ts
|
|
2
|
+
var queueQstash = (props) => {
|
|
3
|
+
const { client, url } = props;
|
|
4
|
+
return {
|
|
5
|
+
name: "qstash",
|
|
6
|
+
version: 1,
|
|
7
|
+
enqueue: async (enqueueProps) => {
|
|
8
|
+
const sent = await client.publishJSON({ url: `${url.replace(/\/$/, "")}/${enqueueProps.name}`, body: enqueueProps.payload, ...enqueueProps.dedupe && { deduplicationId: enqueueProps.dedupe.id }, ...enqueueProps.delayMs !== void 0 && { delay: Math.ceil(enqueueProps.delayMs / 1e3) } });
|
|
9
|
+
return { id: sent.messageId };
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { queueQstash };
|