@promptlycms/prompts 0.1.1 → 0.1.2
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 +13 -21
- package/dist/cli.js +0 -3
- package/dist/index.cjs +1 -300
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -24
- package/dist/schema.d.cts +1 -1
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +279 -4
- package/dist/{types-W-omtl43.d.cts → types-BbNpKJej.d.cts} +1 -16
- package/dist/{types-W-omtl43.d.ts → types-BbNpKJej.d.ts} +1 -16
- package/package.json +1 -1
- package/dist/chunk-GZIUWN6H.js +0 -284
package/README.md
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
# @promptlycms/prompts
|
|
2
2
|
|
|
3
|
-
TypeScript SDK for the [Promptly CMS](https://promptlycms.com) API.
|
|
3
|
+
TypeScript SDK for the [Promptly CMS](https://promptlycms.com) API. Stop hardcoding prompts in your codebase — manage them in a purpose-built CMS with versioning and instant publishing, then fetch them at runtime with full type safety.
|
|
4
4
|
|
|
5
|
-
- **
|
|
5
|
+
- **Zero hardcoded prompts** — fetch prompts at runtime; update wording, models, and settings from the [CMS](https://promptlycms.com) without code changes or redeploys
|
|
6
|
+
- **Runtime client** — `getPrompt()` and `getPrompts()` with full TypeScript support
|
|
6
7
|
- **Codegen CLI** — generates typed template variables via declaration merging
|
|
7
|
-
- **AI SDK integration** —
|
|
8
|
-
- **
|
|
8
|
+
- **AI SDK integration** — destructure directly into [Vercel AI SDK](https://ai-sdk.dev/) `generateText` / `streamText`
|
|
9
|
+
- **Any AI provider** — supports [all providers](https://ai-sdk.dev/providers/ai-sdk-providers#provider-support) supported by the Vercel AI SDK
|
|
9
10
|
- **Structured output** — Zod schemas built from CMS-defined output schemas
|
|
10
11
|
|
|
11
12
|
## Install
|
|
@@ -117,22 +118,22 @@ welcomePrompt.userMessage({ email: 'a@b.com', subject: 'Hi' });
|
|
|
117
118
|
|
|
118
119
|
## AI SDK integration
|
|
119
120
|
|
|
120
|
-
`
|
|
121
|
+
Destructure `getPrompt()` and pass the properties directly to Vercel AI SDK functions:
|
|
121
122
|
|
|
122
123
|
```typescript
|
|
123
124
|
import { generateText } from 'ai';
|
|
124
125
|
|
|
125
|
-
const
|
|
126
|
-
variables: { name: 'Alice', task: 'coding' },
|
|
127
|
-
});
|
|
126
|
+
const { userMessage, systemMessage, temperature, model } = await promptly.getPrompt('my-prompt');
|
|
128
127
|
|
|
129
128
|
const { text } = await generateText({
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
model,
|
|
130
|
+
system: systemMessage,
|
|
131
|
+
prompt: userMessage({ name: 'Alice', task: 'coding' }),
|
|
132
|
+
temperature,
|
|
132
133
|
});
|
|
133
134
|
```
|
|
134
135
|
|
|
135
|
-
The model configured in the CMS is auto-resolved to the correct AI SDK provider.
|
|
136
|
+
The model configured in the CMS is auto-resolved to the correct AI SDK provider.
|
|
136
137
|
|
|
137
138
|
## Model auto-detection
|
|
138
139
|
|
|
@@ -220,7 +221,7 @@ try {
|
|
|
220
221
|
| `baseUrl` | `string` | No | API base URL (default: `https://api.promptlycms.com`) |
|
|
221
222
|
| `model` | `(modelId: string) => LanguageModel` | No | Custom model resolver — overrides auto-detection |
|
|
222
223
|
|
|
223
|
-
Returns a `PromptlyClient` with `getPrompt()
|
|
224
|
+
Returns a `PromptlyClient` with `getPrompt()` and `getPrompts()` methods.
|
|
224
225
|
|
|
225
226
|
### `client.getPrompt(promptId, options?)`
|
|
226
227
|
|
|
@@ -234,15 +235,6 @@ Fetch a single prompt. Returns `PromptResult` with typed `userMessage` when code
|
|
|
234
235
|
|
|
235
236
|
Fetch multiple prompts in parallel. Accepts `PromptRequest[]` and returns a typed tuple matching the input order.
|
|
236
237
|
|
|
237
|
-
### `client.aiParams(promptId, options?)`
|
|
238
|
-
|
|
239
|
-
Fetch a prompt and return params ready to spread into AI SDK functions (`system`, `prompt`, `temperature`, `model`, and optionally `output`).
|
|
240
|
-
|
|
241
|
-
| Option | Type | Description |
|
|
242
|
-
|-------------|-------------------------|--------------------------------|
|
|
243
|
-
| `version` | `string` | Specific version to fetch |
|
|
244
|
-
| `variables` | `Record<string, string>` | Template variables to interpolate |
|
|
245
|
-
|
|
246
238
|
### `@promptlycms/prompts/schema`
|
|
247
239
|
|
|
248
240
|
Subpath export for working with Zod schemas from CMS schema fields:
|
package/dist/cli.js
CHANGED
|
@@ -43,9 +43,6 @@ var createErrorFromResponse = async (response) => {
|
|
|
43
43
|
import { writeFile } from "fs/promises";
|
|
44
44
|
import { createRequire } from "module";
|
|
45
45
|
|
|
46
|
-
// src/schema/builder.ts
|
|
47
|
-
import { z } from "zod";
|
|
48
|
-
|
|
49
46
|
// src/client.ts
|
|
50
47
|
var PROVIDER_PREFIXES = [
|
|
51
48
|
["claude", "anthropic"],
|
package/dist/index.cjs
CHANGED
|
@@ -71,286 +71,6 @@ var createErrorFromResponse = async (response) => {
|
|
|
71
71
|
}
|
|
72
72
|
};
|
|
73
73
|
|
|
74
|
-
// src/schema/builder.ts
|
|
75
|
-
var import_zod = require("zod");
|
|
76
|
-
var resolveTypeString = (typeStr) => {
|
|
77
|
-
const builder = TYPE_BUILDERS.get(typeStr);
|
|
78
|
-
if (builder) {
|
|
79
|
-
return builder({
|
|
80
|
-
id: "",
|
|
81
|
-
name: "",
|
|
82
|
-
type: typeStr,
|
|
83
|
-
validations: [],
|
|
84
|
-
params: {}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
return import_zod.z.string();
|
|
88
|
-
};
|
|
89
|
-
var buildCoercible = (params, coerced, standard) => {
|
|
90
|
-
if (params.coerce) {
|
|
91
|
-
return coerced;
|
|
92
|
-
}
|
|
93
|
-
return standard;
|
|
94
|
-
};
|
|
95
|
-
var TYPE_BUILDERS = /* @__PURE__ */ new Map([
|
|
96
|
-
["string", (f) => buildCoercible(f.params, import_zod.z.coerce.string(), import_zod.z.string())],
|
|
97
|
-
["number", (f) => buildCoercible(f.params, import_zod.z.coerce.number(), import_zod.z.number())],
|
|
98
|
-
["boolean", (f) => buildCoercible(f.params, import_zod.z.coerce.boolean(), import_zod.z.boolean())],
|
|
99
|
-
["date", (f) => buildCoercible(f.params, import_zod.z.coerce.date(), import_zod.z.date())],
|
|
100
|
-
["bigint", (f) => buildCoercible(f.params, import_zod.z.coerce.bigint(), import_zod.z.bigint())],
|
|
101
|
-
["null", () => import_zod.z.null()],
|
|
102
|
-
["undefined", () => import_zod.z.undefined()],
|
|
103
|
-
["void", () => import_zod.z.void()],
|
|
104
|
-
["any", () => import_zod.z.any()],
|
|
105
|
-
["unknown", () => import_zod.z.unknown()],
|
|
106
|
-
["never", () => import_zod.z.never()],
|
|
107
|
-
["nan", () => import_zod.z.nan()],
|
|
108
|
-
["symbol", () => import_zod.z.symbol()],
|
|
109
|
-
[
|
|
110
|
-
"enum",
|
|
111
|
-
(f) => {
|
|
112
|
-
const values = f.params.enumValues ?? [];
|
|
113
|
-
return import_zod.z.enum(values);
|
|
114
|
-
}
|
|
115
|
-
],
|
|
116
|
-
[
|
|
117
|
-
"literal",
|
|
118
|
-
(f) => {
|
|
119
|
-
const value = f.params.enumValues?.[0] ?? "";
|
|
120
|
-
return import_zod.z.literal(value);
|
|
121
|
-
}
|
|
122
|
-
],
|
|
123
|
-
[
|
|
124
|
-
"array",
|
|
125
|
-
(f) => {
|
|
126
|
-
if (f.params.isTuple && f.params.tupleTypes) {
|
|
127
|
-
const types = f.params.tupleTypes.map(resolveTypeString);
|
|
128
|
-
return import_zod.z.tuple(types);
|
|
129
|
-
}
|
|
130
|
-
const elementSchema = f.params.elementType ? resolveTypeString(f.params.elementType) : import_zod.z.unknown();
|
|
131
|
-
return import_zod.z.array(elementSchema);
|
|
132
|
-
}
|
|
133
|
-
],
|
|
134
|
-
[
|
|
135
|
-
"object",
|
|
136
|
-
(f) => {
|
|
137
|
-
const schema = import_zod.z.object({});
|
|
138
|
-
if (f.params.isStrict) {
|
|
139
|
-
return schema.strict();
|
|
140
|
-
}
|
|
141
|
-
if (f.params.isPassthrough) {
|
|
142
|
-
return schema.passthrough();
|
|
143
|
-
}
|
|
144
|
-
return schema;
|
|
145
|
-
}
|
|
146
|
-
],
|
|
147
|
-
[
|
|
148
|
-
"record",
|
|
149
|
-
(f) => {
|
|
150
|
-
const keySchema = f.params.keyType ? resolveTypeString(f.params.keyType) : import_zod.z.string();
|
|
151
|
-
const valueSchema = f.params.valueType ? resolveTypeString(f.params.valueType) : import_zod.z.unknown();
|
|
152
|
-
return import_zod.z.record(keySchema, valueSchema);
|
|
153
|
-
}
|
|
154
|
-
],
|
|
155
|
-
[
|
|
156
|
-
"map",
|
|
157
|
-
(f) => {
|
|
158
|
-
const keySchema = f.params.keyType ? resolveTypeString(f.params.keyType) : import_zod.z.string();
|
|
159
|
-
const valueSchema = f.params.valueType ? resolveTypeString(f.params.valueType) : import_zod.z.unknown();
|
|
160
|
-
return import_zod.z.map(keySchema, valueSchema);
|
|
161
|
-
}
|
|
162
|
-
],
|
|
163
|
-
[
|
|
164
|
-
"set",
|
|
165
|
-
(f) => {
|
|
166
|
-
const elementSchema = f.params.elementType ? resolveTypeString(f.params.elementType) : import_zod.z.unknown();
|
|
167
|
-
return import_zod.z.set(elementSchema);
|
|
168
|
-
}
|
|
169
|
-
],
|
|
170
|
-
[
|
|
171
|
-
"union",
|
|
172
|
-
(f) => {
|
|
173
|
-
if (f.params.isDiscriminatedUnion && f.params.discriminatedUnion) {
|
|
174
|
-
const { discriminator, cases } = f.params.discriminatedUnion;
|
|
175
|
-
const schemas = Object.values(cases).map(
|
|
176
|
-
(c) => import_zod.z.object({
|
|
177
|
-
[discriminator]: import_zod.z.literal(c.value),
|
|
178
|
-
...Object.fromEntries(
|
|
179
|
-
c.fields.map((field) => [field.name, buildFieldSchema(field)])
|
|
180
|
-
)
|
|
181
|
-
})
|
|
182
|
-
);
|
|
183
|
-
return import_zod.z.discriminatedUnion(
|
|
184
|
-
discriminator,
|
|
185
|
-
schemas
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
const types = (f.params.unionTypes ?? []).map(resolveTypeString);
|
|
189
|
-
return import_zod.z.union(types);
|
|
190
|
-
}
|
|
191
|
-
],
|
|
192
|
-
[
|
|
193
|
-
"intersection",
|
|
194
|
-
(f) => {
|
|
195
|
-
const types = (f.params.unionTypes ?? []).map(resolveTypeString);
|
|
196
|
-
return import_zod.z.intersection(types[0] ?? import_zod.z.unknown(), types[1] ?? import_zod.z.unknown());
|
|
197
|
-
}
|
|
198
|
-
]
|
|
199
|
-
]);
|
|
200
|
-
var coerceDefaultValue = (value, fieldType) => {
|
|
201
|
-
const coercers = /* @__PURE__ */ new Map([
|
|
202
|
-
["number", (v) => Number(v)],
|
|
203
|
-
["boolean", (v) => v === "true"],
|
|
204
|
-
["bigint", (v) => BigInt(v)]
|
|
205
|
-
]);
|
|
206
|
-
const coercer = coercers.get(fieldType);
|
|
207
|
-
if (coercer) {
|
|
208
|
-
return coercer(value);
|
|
209
|
-
}
|
|
210
|
-
return value;
|
|
211
|
-
};
|
|
212
|
-
var applySizeValidation = (schema, type, value, message) => {
|
|
213
|
-
const num = Number(value);
|
|
214
|
-
if (schema instanceof import_zod.z.ZodString) {
|
|
215
|
-
const methods = {
|
|
216
|
-
min: (n, m) => schema.min(n, m),
|
|
217
|
-
max: (n, m) => schema.max(n, m),
|
|
218
|
-
length: (n, m) => schema.length(n, m)
|
|
219
|
-
};
|
|
220
|
-
return methods[type]?.(num, message) ?? schema;
|
|
221
|
-
}
|
|
222
|
-
if (schema instanceof import_zod.z.ZodNumber) {
|
|
223
|
-
const methods = {
|
|
224
|
-
min: (n, m) => schema.min(n, m),
|
|
225
|
-
max: (n, m) => schema.max(n, m)
|
|
226
|
-
};
|
|
227
|
-
return methods[type]?.(num, message) ?? schema;
|
|
228
|
-
}
|
|
229
|
-
if (schema instanceof import_zod.z.ZodArray) {
|
|
230
|
-
const methods = {
|
|
231
|
-
min: (n) => schema.min(n),
|
|
232
|
-
max: (n) => schema.max(n),
|
|
233
|
-
length: (n) => schema.length(n)
|
|
234
|
-
};
|
|
235
|
-
return methods[type]?.(num) ?? schema;
|
|
236
|
-
}
|
|
237
|
-
return schema;
|
|
238
|
-
};
|
|
239
|
-
var VALIDATION_APPLICATORS = /* @__PURE__ */ new Map([
|
|
240
|
-
// Size validations
|
|
241
|
-
["min", (s, r) => applySizeValidation(s, "min", r.value, r.message)],
|
|
242
|
-
["max", (s, r) => applySizeValidation(s, "max", r.value, r.message)],
|
|
243
|
-
["length", (s, r) => applySizeValidation(s, "length", r.value, r.message)],
|
|
244
|
-
// String validations
|
|
245
|
-
["email", (s, r) => s instanceof import_zod.z.ZodString ? s.email(r.message) : s],
|
|
246
|
-
["url", (s, r) => s instanceof import_zod.z.ZodString ? s.url(r.message) : s],
|
|
247
|
-
["uuid", (s, r) => s instanceof import_zod.z.ZodString ? s.uuid(r.message) : s],
|
|
248
|
-
["cuid", (s, r) => s instanceof import_zod.z.ZodString ? s.cuid(r.message) : s],
|
|
249
|
-
["cuid2", (s, r) => s instanceof import_zod.z.ZodString ? s.cuid2(r.message) : s],
|
|
250
|
-
["ulid", (s, r) => s instanceof import_zod.z.ZodString ? s.ulid(r.message) : s],
|
|
251
|
-
[
|
|
252
|
-
"regex",
|
|
253
|
-
(s, r) => s instanceof import_zod.z.ZodString ? s.regex(new RegExp(r.value), r.message) : s
|
|
254
|
-
],
|
|
255
|
-
[
|
|
256
|
-
"startsWith",
|
|
257
|
-
(s, r) => s instanceof import_zod.z.ZodString ? s.startsWith(r.value, r.message) : s
|
|
258
|
-
],
|
|
259
|
-
[
|
|
260
|
-
"endsWith",
|
|
261
|
-
(s, r) => s instanceof import_zod.z.ZodString ? s.endsWith(r.value, r.message) : s
|
|
262
|
-
],
|
|
263
|
-
[
|
|
264
|
-
"datetime",
|
|
265
|
-
(s, _r, f) => {
|
|
266
|
-
if (!(s instanceof import_zod.z.ZodString)) {
|
|
267
|
-
return s;
|
|
268
|
-
}
|
|
269
|
-
const opts = f.params.stringOptions?.datetime;
|
|
270
|
-
return s.datetime(opts);
|
|
271
|
-
}
|
|
272
|
-
],
|
|
273
|
-
[
|
|
274
|
-
"ip",
|
|
275
|
-
(s, _r, f) => {
|
|
276
|
-
if (!(s instanceof import_zod.z.ZodString)) {
|
|
277
|
-
return s;
|
|
278
|
-
}
|
|
279
|
-
const version = f.params.stringOptions?.ip?.version;
|
|
280
|
-
if (version === "v6") {
|
|
281
|
-
return s.ipv6();
|
|
282
|
-
}
|
|
283
|
-
return s.ipv4();
|
|
284
|
-
}
|
|
285
|
-
],
|
|
286
|
-
// String transforms
|
|
287
|
-
["trim", (s) => s instanceof import_zod.z.ZodString ? s.trim() : s],
|
|
288
|
-
["toLowerCase", (s) => s instanceof import_zod.z.ZodString ? s.toLowerCase() : s],
|
|
289
|
-
["toUpperCase", (s) => s instanceof import_zod.z.ZodString ? s.toUpperCase() : s],
|
|
290
|
-
// Number validations
|
|
291
|
-
["int", (s, r) => s instanceof import_zod.z.ZodNumber ? s.int(r.message) : s],
|
|
292
|
-
[
|
|
293
|
-
"positive",
|
|
294
|
-
(s, r) => s instanceof import_zod.z.ZodNumber ? s.positive(r.message) : s
|
|
295
|
-
],
|
|
296
|
-
[
|
|
297
|
-
"negative",
|
|
298
|
-
(s, r) => s instanceof import_zod.z.ZodNumber ? s.negative(r.message) : s
|
|
299
|
-
],
|
|
300
|
-
[
|
|
301
|
-
"multipleOf",
|
|
302
|
-
(s, r) => s instanceof import_zod.z.ZodNumber ? s.multipleOf(Number(r.value), r.message) : s
|
|
303
|
-
],
|
|
304
|
-
["finite", (s, r) => s instanceof import_zod.z.ZodNumber ? s.finite(r.message) : s],
|
|
305
|
-
["safe", (s, r) => s instanceof import_zod.z.ZodNumber ? s.safe(r.message) : s],
|
|
306
|
-
// Collection
|
|
307
|
-
[
|
|
308
|
-
"nonempty",
|
|
309
|
-
(s) => {
|
|
310
|
-
if (s instanceof import_zod.z.ZodString) {
|
|
311
|
-
return s.min(1);
|
|
312
|
-
}
|
|
313
|
-
if (s instanceof import_zod.z.ZodArray) {
|
|
314
|
-
return s.nonempty();
|
|
315
|
-
}
|
|
316
|
-
return s;
|
|
317
|
-
}
|
|
318
|
-
],
|
|
319
|
-
// Wrapping modifiers
|
|
320
|
-
["optional", (s) => s.optional()],
|
|
321
|
-
["nullable", (s) => s.nullable()],
|
|
322
|
-
["nullish", (s) => s.nullish()],
|
|
323
|
-
// Default & catch
|
|
324
|
-
["default", (s, r, f) => s.default(coerceDefaultValue(r.value, f.type))],
|
|
325
|
-
["catch", (s, r, f) => s.catch(coerceDefaultValue(r.value, f.type))],
|
|
326
|
-
// Readonly
|
|
327
|
-
["readonly", (s) => s.readonly()]
|
|
328
|
-
]);
|
|
329
|
-
var buildFieldSchema = (field) => {
|
|
330
|
-
const builder = TYPE_BUILDERS.get(field.type);
|
|
331
|
-
if (!builder) {
|
|
332
|
-
return import_zod.z.unknown();
|
|
333
|
-
}
|
|
334
|
-
let schema = builder(field);
|
|
335
|
-
for (const rule of field.validations) {
|
|
336
|
-
const applicator = VALIDATION_APPLICATORS.get(rule.type);
|
|
337
|
-
if (applicator) {
|
|
338
|
-
schema = applicator(schema, rule, field);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
if (field.params.description) {
|
|
342
|
-
schema = schema.describe(field.params.description);
|
|
343
|
-
}
|
|
344
|
-
return schema;
|
|
345
|
-
};
|
|
346
|
-
var buildZodSchema = (fields) => {
|
|
347
|
-
const shape = {};
|
|
348
|
-
for (const field of fields) {
|
|
349
|
-
shape[field.name] = buildFieldSchema(field);
|
|
350
|
-
}
|
|
351
|
-
return import_zod.z.object(shape);
|
|
352
|
-
};
|
|
353
|
-
|
|
354
74
|
// src/client.ts
|
|
355
75
|
var DEFAULT_BASE_URL = "https://api.promptlycms.com";
|
|
356
76
|
var MODEL_ID_MAP = {
|
|
@@ -503,26 +223,7 @@ var createPromptlyClient = (config) => {
|
|
|
503
223
|
);
|
|
504
224
|
return results;
|
|
505
225
|
};
|
|
506
|
-
|
|
507
|
-
const prompt = await fetchPrompt(promptId, {
|
|
508
|
-
version: options?.version
|
|
509
|
-
});
|
|
510
|
-
const userMessage = options?.variables ? interpolate(prompt.userMessage, options.variables) : prompt.userMessage;
|
|
511
|
-
const model = await modelResolver(prompt.config.model);
|
|
512
|
-
const result = {
|
|
513
|
-
system: prompt.systemMessage,
|
|
514
|
-
prompt: userMessage,
|
|
515
|
-
temperature: prompt.config.temperature,
|
|
516
|
-
model
|
|
517
|
-
};
|
|
518
|
-
if (prompt.config.schema && prompt.config.schema.length > 0) {
|
|
519
|
-
const schema = buildZodSchema(prompt.config.schema);
|
|
520
|
-
const { Output } = await import("ai");
|
|
521
|
-
result.output = Output.object({ schema });
|
|
522
|
-
}
|
|
523
|
-
return result;
|
|
524
|
-
};
|
|
525
|
-
return { getPrompt, getPrompts, aiParams };
|
|
226
|
+
return { getPrompt, getPrompts };
|
|
526
227
|
};
|
|
527
228
|
// Annotate the CommonJS export names for ESM import in node:
|
|
528
229
|
0 && (module.exports = {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { P as PromptlyClientConfig, a as PromptlyClient, E as ErrorCode } from './types-
|
|
2
|
-
export {
|
|
1
|
+
import { P as PromptlyClientConfig, a as PromptlyClient, E as ErrorCode } from './types-BbNpKJej.cjs';
|
|
2
|
+
export { b as ErrorResponse, G as GetOptions, c as PromptConfig, d as PromptId, e as PromptMessage, f as PromptRequest, g as PromptResponse, h as PromptResult, i as PromptVariableMap, j as PromptVersion, k as PublishedVersion, S as SchemaField, l as SchemaFieldParams, V as ValidationRule } from './types-BbNpKJej.cjs';
|
|
3
3
|
import 'ai';
|
|
4
4
|
|
|
5
5
|
declare const getSdkModelId: (modelId: string) => string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { P as PromptlyClientConfig, a as PromptlyClient, E as ErrorCode } from './types-
|
|
2
|
-
export {
|
|
1
|
+
import { P as PromptlyClientConfig, a as PromptlyClient, E as ErrorCode } from './types-BbNpKJej.js';
|
|
2
|
+
export { b as ErrorResponse, G as GetOptions, c as PromptConfig, d as PromptId, e as PromptMessage, f as PromptRequest, g as PromptResponse, h as PromptResult, i as PromptVariableMap, j as PromptVersion, k as PublishedVersion, S as SchemaField, l as SchemaFieldParams, V as ValidationRule } from './types-BbNpKJej.js';
|
|
3
3
|
import 'ai';
|
|
4
4
|
|
|
5
5
|
declare const getSdkModelId: (modelId: string) => string;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildZodSchema
|
|
3
|
-
} from "./chunk-GZIUWN6H.js";
|
|
4
|
-
|
|
5
1
|
// src/errors.ts
|
|
6
2
|
var PromptlyError = class extends Error {
|
|
7
3
|
code;
|
|
@@ -188,26 +184,7 @@ var createPromptlyClient = (config) => {
|
|
|
188
184
|
);
|
|
189
185
|
return results;
|
|
190
186
|
};
|
|
191
|
-
|
|
192
|
-
const prompt = await fetchPrompt(promptId, {
|
|
193
|
-
version: options?.version
|
|
194
|
-
});
|
|
195
|
-
const userMessage = options?.variables ? interpolate(prompt.userMessage, options.variables) : prompt.userMessage;
|
|
196
|
-
const model = await modelResolver(prompt.config.model);
|
|
197
|
-
const result = {
|
|
198
|
-
system: prompt.systemMessage,
|
|
199
|
-
prompt: userMessage,
|
|
200
|
-
temperature: prompt.config.temperature,
|
|
201
|
-
model
|
|
202
|
-
};
|
|
203
|
-
if (prompt.config.schema && prompt.config.schema.length > 0) {
|
|
204
|
-
const schema = buildZodSchema(prompt.config.schema);
|
|
205
|
-
const { Output } = await import("ai");
|
|
206
|
-
result.output = Output.object({ schema });
|
|
207
|
-
}
|
|
208
|
-
return result;
|
|
209
|
-
};
|
|
210
|
-
return { getPrompt, getPrompts, aiParams };
|
|
187
|
+
return { getPrompt, getPrompts };
|
|
211
188
|
};
|
|
212
189
|
export {
|
|
213
190
|
PromptlyError,
|
package/dist/schema.d.cts
CHANGED
package/dist/schema.d.ts
CHANGED
package/dist/schema.js
CHANGED
|
@@ -1,7 +1,282 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// src/schema/builder.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
var resolveTypeString = (typeStr) => {
|
|
4
|
+
const builder = TYPE_BUILDERS.get(typeStr);
|
|
5
|
+
if (builder) {
|
|
6
|
+
return builder({
|
|
7
|
+
id: "",
|
|
8
|
+
name: "",
|
|
9
|
+
type: typeStr,
|
|
10
|
+
validations: [],
|
|
11
|
+
params: {}
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
return z.string();
|
|
15
|
+
};
|
|
16
|
+
var buildCoercible = (params, coerced, standard) => {
|
|
17
|
+
if (params.coerce) {
|
|
18
|
+
return coerced;
|
|
19
|
+
}
|
|
20
|
+
return standard;
|
|
21
|
+
};
|
|
22
|
+
var TYPE_BUILDERS = /* @__PURE__ */ new Map([
|
|
23
|
+
["string", (f) => buildCoercible(f.params, z.coerce.string(), z.string())],
|
|
24
|
+
["number", (f) => buildCoercible(f.params, z.coerce.number(), z.number())],
|
|
25
|
+
["boolean", (f) => buildCoercible(f.params, z.coerce.boolean(), z.boolean())],
|
|
26
|
+
["date", (f) => buildCoercible(f.params, z.coerce.date(), z.date())],
|
|
27
|
+
["bigint", (f) => buildCoercible(f.params, z.coerce.bigint(), z.bigint())],
|
|
28
|
+
["null", () => z.null()],
|
|
29
|
+
["undefined", () => z.undefined()],
|
|
30
|
+
["void", () => z.void()],
|
|
31
|
+
["any", () => z.any()],
|
|
32
|
+
["unknown", () => z.unknown()],
|
|
33
|
+
["never", () => z.never()],
|
|
34
|
+
["nan", () => z.nan()],
|
|
35
|
+
["symbol", () => z.symbol()],
|
|
36
|
+
[
|
|
37
|
+
"enum",
|
|
38
|
+
(f) => {
|
|
39
|
+
const values = f.params.enumValues ?? [];
|
|
40
|
+
return z.enum(values);
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
[
|
|
44
|
+
"literal",
|
|
45
|
+
(f) => {
|
|
46
|
+
const value = f.params.enumValues?.[0] ?? "";
|
|
47
|
+
return z.literal(value);
|
|
48
|
+
}
|
|
49
|
+
],
|
|
50
|
+
[
|
|
51
|
+
"array",
|
|
52
|
+
(f) => {
|
|
53
|
+
if (f.params.isTuple && f.params.tupleTypes) {
|
|
54
|
+
const types = f.params.tupleTypes.map(resolveTypeString);
|
|
55
|
+
return z.tuple(types);
|
|
56
|
+
}
|
|
57
|
+
const elementSchema = f.params.elementType ? resolveTypeString(f.params.elementType) : z.unknown();
|
|
58
|
+
return z.array(elementSchema);
|
|
59
|
+
}
|
|
60
|
+
],
|
|
61
|
+
[
|
|
62
|
+
"object",
|
|
63
|
+
(f) => {
|
|
64
|
+
const schema = z.object({});
|
|
65
|
+
if (f.params.isStrict) {
|
|
66
|
+
return schema.strict();
|
|
67
|
+
}
|
|
68
|
+
if (f.params.isPassthrough) {
|
|
69
|
+
return schema.passthrough();
|
|
70
|
+
}
|
|
71
|
+
return schema;
|
|
72
|
+
}
|
|
73
|
+
],
|
|
74
|
+
[
|
|
75
|
+
"record",
|
|
76
|
+
(f) => {
|
|
77
|
+
const keySchema = f.params.keyType ? resolveTypeString(f.params.keyType) : z.string();
|
|
78
|
+
const valueSchema = f.params.valueType ? resolveTypeString(f.params.valueType) : z.unknown();
|
|
79
|
+
return z.record(keySchema, valueSchema);
|
|
80
|
+
}
|
|
81
|
+
],
|
|
82
|
+
[
|
|
83
|
+
"map",
|
|
84
|
+
(f) => {
|
|
85
|
+
const keySchema = f.params.keyType ? resolveTypeString(f.params.keyType) : z.string();
|
|
86
|
+
const valueSchema = f.params.valueType ? resolveTypeString(f.params.valueType) : z.unknown();
|
|
87
|
+
return z.map(keySchema, valueSchema);
|
|
88
|
+
}
|
|
89
|
+
],
|
|
90
|
+
[
|
|
91
|
+
"set",
|
|
92
|
+
(f) => {
|
|
93
|
+
const elementSchema = f.params.elementType ? resolveTypeString(f.params.elementType) : z.unknown();
|
|
94
|
+
return z.set(elementSchema);
|
|
95
|
+
}
|
|
96
|
+
],
|
|
97
|
+
[
|
|
98
|
+
"union",
|
|
99
|
+
(f) => {
|
|
100
|
+
if (f.params.isDiscriminatedUnion && f.params.discriminatedUnion) {
|
|
101
|
+
const { discriminator, cases } = f.params.discriminatedUnion;
|
|
102
|
+
const schemas = Object.values(cases).map(
|
|
103
|
+
(c) => z.object({
|
|
104
|
+
[discriminator]: z.literal(c.value),
|
|
105
|
+
...Object.fromEntries(
|
|
106
|
+
c.fields.map((field) => [field.name, buildFieldSchema(field)])
|
|
107
|
+
)
|
|
108
|
+
})
|
|
109
|
+
);
|
|
110
|
+
return z.discriminatedUnion(
|
|
111
|
+
discriminator,
|
|
112
|
+
schemas
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
const types = (f.params.unionTypes ?? []).map(resolveTypeString);
|
|
116
|
+
return z.union(types);
|
|
117
|
+
}
|
|
118
|
+
],
|
|
119
|
+
[
|
|
120
|
+
"intersection",
|
|
121
|
+
(f) => {
|
|
122
|
+
const types = (f.params.unionTypes ?? []).map(resolveTypeString);
|
|
123
|
+
return z.intersection(types[0] ?? z.unknown(), types[1] ?? z.unknown());
|
|
124
|
+
}
|
|
125
|
+
]
|
|
126
|
+
]);
|
|
127
|
+
var coerceDefaultValue = (value, fieldType) => {
|
|
128
|
+
const coercers = /* @__PURE__ */ new Map([
|
|
129
|
+
["number", (v) => Number(v)],
|
|
130
|
+
["boolean", (v) => v === "true"],
|
|
131
|
+
["bigint", (v) => BigInt(v)]
|
|
132
|
+
]);
|
|
133
|
+
const coercer = coercers.get(fieldType);
|
|
134
|
+
if (coercer) {
|
|
135
|
+
return coercer(value);
|
|
136
|
+
}
|
|
137
|
+
return value;
|
|
138
|
+
};
|
|
139
|
+
var applySizeValidation = (schema, type, value, message) => {
|
|
140
|
+
const num = Number(value);
|
|
141
|
+
if (schema instanceof z.ZodString) {
|
|
142
|
+
const methods = {
|
|
143
|
+
min: (n, m) => schema.min(n, m),
|
|
144
|
+
max: (n, m) => schema.max(n, m),
|
|
145
|
+
length: (n, m) => schema.length(n, m)
|
|
146
|
+
};
|
|
147
|
+
return methods[type]?.(num, message) ?? schema;
|
|
148
|
+
}
|
|
149
|
+
if (schema instanceof z.ZodNumber) {
|
|
150
|
+
const methods = {
|
|
151
|
+
min: (n, m) => schema.min(n, m),
|
|
152
|
+
max: (n, m) => schema.max(n, m)
|
|
153
|
+
};
|
|
154
|
+
return methods[type]?.(num, message) ?? schema;
|
|
155
|
+
}
|
|
156
|
+
if (schema instanceof z.ZodArray) {
|
|
157
|
+
const methods = {
|
|
158
|
+
min: (n) => schema.min(n),
|
|
159
|
+
max: (n) => schema.max(n),
|
|
160
|
+
length: (n) => schema.length(n)
|
|
161
|
+
};
|
|
162
|
+
return methods[type]?.(num) ?? schema;
|
|
163
|
+
}
|
|
164
|
+
return schema;
|
|
165
|
+
};
|
|
166
|
+
var VALIDATION_APPLICATORS = /* @__PURE__ */ new Map([
|
|
167
|
+
// Size validations
|
|
168
|
+
["min", (s, r) => applySizeValidation(s, "min", r.value, r.message)],
|
|
169
|
+
["max", (s, r) => applySizeValidation(s, "max", r.value, r.message)],
|
|
170
|
+
["length", (s, r) => applySizeValidation(s, "length", r.value, r.message)],
|
|
171
|
+
// String validations
|
|
172
|
+
["email", (s, r) => s instanceof z.ZodString ? s.email(r.message) : s],
|
|
173
|
+
["url", (s, r) => s instanceof z.ZodString ? s.url(r.message) : s],
|
|
174
|
+
["uuid", (s, r) => s instanceof z.ZodString ? s.uuid(r.message) : s],
|
|
175
|
+
["cuid", (s, r) => s instanceof z.ZodString ? s.cuid(r.message) : s],
|
|
176
|
+
["cuid2", (s, r) => s instanceof z.ZodString ? s.cuid2(r.message) : s],
|
|
177
|
+
["ulid", (s, r) => s instanceof z.ZodString ? s.ulid(r.message) : s],
|
|
178
|
+
[
|
|
179
|
+
"regex",
|
|
180
|
+
(s, r) => s instanceof z.ZodString ? s.regex(new RegExp(r.value), r.message) : s
|
|
181
|
+
],
|
|
182
|
+
[
|
|
183
|
+
"startsWith",
|
|
184
|
+
(s, r) => s instanceof z.ZodString ? s.startsWith(r.value, r.message) : s
|
|
185
|
+
],
|
|
186
|
+
[
|
|
187
|
+
"endsWith",
|
|
188
|
+
(s, r) => s instanceof z.ZodString ? s.endsWith(r.value, r.message) : s
|
|
189
|
+
],
|
|
190
|
+
[
|
|
191
|
+
"datetime",
|
|
192
|
+
(s, _r, f) => {
|
|
193
|
+
if (!(s instanceof z.ZodString)) {
|
|
194
|
+
return s;
|
|
195
|
+
}
|
|
196
|
+
const opts = f.params.stringOptions?.datetime;
|
|
197
|
+
return s.datetime(opts);
|
|
198
|
+
}
|
|
199
|
+
],
|
|
200
|
+
[
|
|
201
|
+
"ip",
|
|
202
|
+
(s, _r, f) => {
|
|
203
|
+
if (!(s instanceof z.ZodString)) {
|
|
204
|
+
return s;
|
|
205
|
+
}
|
|
206
|
+
const version = f.params.stringOptions?.ip?.version;
|
|
207
|
+
if (version === "v6") {
|
|
208
|
+
return s.ipv6();
|
|
209
|
+
}
|
|
210
|
+
return s.ipv4();
|
|
211
|
+
}
|
|
212
|
+
],
|
|
213
|
+
// String transforms
|
|
214
|
+
["trim", (s) => s instanceof z.ZodString ? s.trim() : s],
|
|
215
|
+
["toLowerCase", (s) => s instanceof z.ZodString ? s.toLowerCase() : s],
|
|
216
|
+
["toUpperCase", (s) => s instanceof z.ZodString ? s.toUpperCase() : s],
|
|
217
|
+
// Number validations
|
|
218
|
+
["int", (s, r) => s instanceof z.ZodNumber ? s.int(r.message) : s],
|
|
219
|
+
[
|
|
220
|
+
"positive",
|
|
221
|
+
(s, r) => s instanceof z.ZodNumber ? s.positive(r.message) : s
|
|
222
|
+
],
|
|
223
|
+
[
|
|
224
|
+
"negative",
|
|
225
|
+
(s, r) => s instanceof z.ZodNumber ? s.negative(r.message) : s
|
|
226
|
+
],
|
|
227
|
+
[
|
|
228
|
+
"multipleOf",
|
|
229
|
+
(s, r) => s instanceof z.ZodNumber ? s.multipleOf(Number(r.value), r.message) : s
|
|
230
|
+
],
|
|
231
|
+
["finite", (s, r) => s instanceof z.ZodNumber ? s.finite(r.message) : s],
|
|
232
|
+
["safe", (s, r) => s instanceof z.ZodNumber ? s.safe(r.message) : s],
|
|
233
|
+
// Collection
|
|
234
|
+
[
|
|
235
|
+
"nonempty",
|
|
236
|
+
(s) => {
|
|
237
|
+
if (s instanceof z.ZodString) {
|
|
238
|
+
return s.min(1);
|
|
239
|
+
}
|
|
240
|
+
if (s instanceof z.ZodArray) {
|
|
241
|
+
return s.nonempty();
|
|
242
|
+
}
|
|
243
|
+
return s;
|
|
244
|
+
}
|
|
245
|
+
],
|
|
246
|
+
// Wrapping modifiers
|
|
247
|
+
["optional", (s) => s.optional()],
|
|
248
|
+
["nullable", (s) => s.nullable()],
|
|
249
|
+
["nullish", (s) => s.nullish()],
|
|
250
|
+
// Default & catch
|
|
251
|
+
["default", (s, r, f) => s.default(coerceDefaultValue(r.value, f.type))],
|
|
252
|
+
["catch", (s, r, f) => s.catch(coerceDefaultValue(r.value, f.type))],
|
|
253
|
+
// Readonly
|
|
254
|
+
["readonly", (s) => s.readonly()]
|
|
255
|
+
]);
|
|
256
|
+
var buildFieldSchema = (field) => {
|
|
257
|
+
const builder = TYPE_BUILDERS.get(field.type);
|
|
258
|
+
if (!builder) {
|
|
259
|
+
return z.unknown();
|
|
260
|
+
}
|
|
261
|
+
let schema = builder(field);
|
|
262
|
+
for (const rule of field.validations) {
|
|
263
|
+
const applicator = VALIDATION_APPLICATORS.get(rule.type);
|
|
264
|
+
if (applicator) {
|
|
265
|
+
schema = applicator(schema, rule, field);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (field.params.description) {
|
|
269
|
+
schema = schema.describe(field.params.description);
|
|
270
|
+
}
|
|
271
|
+
return schema;
|
|
272
|
+
};
|
|
273
|
+
var buildZodSchema = (fields) => {
|
|
274
|
+
const shape = {};
|
|
275
|
+
for (const field of fields) {
|
|
276
|
+
shape[field.name] = buildFieldSchema(field);
|
|
277
|
+
}
|
|
278
|
+
return z.object(shape);
|
|
279
|
+
};
|
|
5
280
|
|
|
6
281
|
// src/schema/codegen.ts
|
|
7
282
|
var escapeString = (str) => str.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n");
|
|
@@ -110,24 +110,9 @@ type PromptlyClientConfig = {
|
|
|
110
110
|
type GetOptions<V extends string = string> = {
|
|
111
111
|
version?: V;
|
|
112
112
|
};
|
|
113
|
-
type AiParamsOptions = {
|
|
114
|
-
version?: string;
|
|
115
|
-
variables?: Record<string, string>;
|
|
116
|
-
};
|
|
117
|
-
type AiParams = {
|
|
118
|
-
system: string;
|
|
119
|
-
prompt: string;
|
|
120
|
-
temperature: number;
|
|
121
|
-
model: ai.LanguageModel;
|
|
122
|
-
output?: ReturnType<typeof ai.Output.object>;
|
|
123
|
-
};
|
|
124
113
|
type PromptlyClient = {
|
|
125
114
|
getPrompt: <T extends string, V extends PromptVersion<T> | 'latest' = 'latest'>(promptId: T, options?: GetOptions<V>) => Promise<PromptResult<VariablesFor<T, V>>>;
|
|
126
115
|
getPrompts: <const T extends readonly PromptRequest[]>(entries: T) => Promise<GetPromptsResults<T>>;
|
|
127
|
-
aiParams: <T extends string, V extends PromptVersion<T> | 'latest' = 'latest'>(promptId: T, options?: {
|
|
128
|
-
version?: V;
|
|
129
|
-
variables?: VariablesFor<T, V>;
|
|
130
|
-
}) => Promise<AiParams>;
|
|
131
116
|
};
|
|
132
117
|
|
|
133
|
-
export type {
|
|
118
|
+
export type { ErrorCode as E, GetOptions as G, PromptlyClientConfig as P, SchemaField as S, ValidationRule as V, PromptlyClient as a, ErrorResponse as b, PromptConfig as c, PromptId as d, PromptMessage as e, PromptRequest as f, PromptResponse as g, PromptResult as h, PromptVariableMap as i, PromptVersion as j, PublishedVersion as k, SchemaFieldParams as l };
|
|
@@ -110,24 +110,9 @@ type PromptlyClientConfig = {
|
|
|
110
110
|
type GetOptions<V extends string = string> = {
|
|
111
111
|
version?: V;
|
|
112
112
|
};
|
|
113
|
-
type AiParamsOptions = {
|
|
114
|
-
version?: string;
|
|
115
|
-
variables?: Record<string, string>;
|
|
116
|
-
};
|
|
117
|
-
type AiParams = {
|
|
118
|
-
system: string;
|
|
119
|
-
prompt: string;
|
|
120
|
-
temperature: number;
|
|
121
|
-
model: ai.LanguageModel;
|
|
122
|
-
output?: ReturnType<typeof ai.Output.object>;
|
|
123
|
-
};
|
|
124
113
|
type PromptlyClient = {
|
|
125
114
|
getPrompt: <T extends string, V extends PromptVersion<T> | 'latest' = 'latest'>(promptId: T, options?: GetOptions<V>) => Promise<PromptResult<VariablesFor<T, V>>>;
|
|
126
115
|
getPrompts: <const T extends readonly PromptRequest[]>(entries: T) => Promise<GetPromptsResults<T>>;
|
|
127
|
-
aiParams: <T extends string, V extends PromptVersion<T> | 'latest' = 'latest'>(promptId: T, options?: {
|
|
128
|
-
version?: V;
|
|
129
|
-
variables?: VariablesFor<T, V>;
|
|
130
|
-
}) => Promise<AiParams>;
|
|
131
116
|
};
|
|
132
117
|
|
|
133
|
-
export type {
|
|
118
|
+
export type { ErrorCode as E, GetOptions as G, PromptlyClientConfig as P, SchemaField as S, ValidationRule as V, PromptlyClient as a, ErrorResponse as b, PromptConfig as c, PromptId as d, PromptMessage as e, PromptRequest as f, PromptResponse as g, PromptResult as h, PromptVariableMap as i, PromptVersion as j, PublishedVersion as k, SchemaFieldParams as l };
|
package/package.json
CHANGED
package/dist/chunk-GZIUWN6H.js
DELETED
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
// src/schema/builder.ts
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
var resolveTypeString = (typeStr) => {
|
|
4
|
-
const builder = TYPE_BUILDERS.get(typeStr);
|
|
5
|
-
if (builder) {
|
|
6
|
-
return builder({
|
|
7
|
-
id: "",
|
|
8
|
-
name: "",
|
|
9
|
-
type: typeStr,
|
|
10
|
-
validations: [],
|
|
11
|
-
params: {}
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
return z.string();
|
|
15
|
-
};
|
|
16
|
-
var buildCoercible = (params, coerced, standard) => {
|
|
17
|
-
if (params.coerce) {
|
|
18
|
-
return coerced;
|
|
19
|
-
}
|
|
20
|
-
return standard;
|
|
21
|
-
};
|
|
22
|
-
var TYPE_BUILDERS = /* @__PURE__ */ new Map([
|
|
23
|
-
["string", (f) => buildCoercible(f.params, z.coerce.string(), z.string())],
|
|
24
|
-
["number", (f) => buildCoercible(f.params, z.coerce.number(), z.number())],
|
|
25
|
-
["boolean", (f) => buildCoercible(f.params, z.coerce.boolean(), z.boolean())],
|
|
26
|
-
["date", (f) => buildCoercible(f.params, z.coerce.date(), z.date())],
|
|
27
|
-
["bigint", (f) => buildCoercible(f.params, z.coerce.bigint(), z.bigint())],
|
|
28
|
-
["null", () => z.null()],
|
|
29
|
-
["undefined", () => z.undefined()],
|
|
30
|
-
["void", () => z.void()],
|
|
31
|
-
["any", () => z.any()],
|
|
32
|
-
["unknown", () => z.unknown()],
|
|
33
|
-
["never", () => z.never()],
|
|
34
|
-
["nan", () => z.nan()],
|
|
35
|
-
["symbol", () => z.symbol()],
|
|
36
|
-
[
|
|
37
|
-
"enum",
|
|
38
|
-
(f) => {
|
|
39
|
-
const values = f.params.enumValues ?? [];
|
|
40
|
-
return z.enum(values);
|
|
41
|
-
}
|
|
42
|
-
],
|
|
43
|
-
[
|
|
44
|
-
"literal",
|
|
45
|
-
(f) => {
|
|
46
|
-
const value = f.params.enumValues?.[0] ?? "";
|
|
47
|
-
return z.literal(value);
|
|
48
|
-
}
|
|
49
|
-
],
|
|
50
|
-
[
|
|
51
|
-
"array",
|
|
52
|
-
(f) => {
|
|
53
|
-
if (f.params.isTuple && f.params.tupleTypes) {
|
|
54
|
-
const types = f.params.tupleTypes.map(resolveTypeString);
|
|
55
|
-
return z.tuple(types);
|
|
56
|
-
}
|
|
57
|
-
const elementSchema = f.params.elementType ? resolveTypeString(f.params.elementType) : z.unknown();
|
|
58
|
-
return z.array(elementSchema);
|
|
59
|
-
}
|
|
60
|
-
],
|
|
61
|
-
[
|
|
62
|
-
"object",
|
|
63
|
-
(f) => {
|
|
64
|
-
const schema = z.object({});
|
|
65
|
-
if (f.params.isStrict) {
|
|
66
|
-
return schema.strict();
|
|
67
|
-
}
|
|
68
|
-
if (f.params.isPassthrough) {
|
|
69
|
-
return schema.passthrough();
|
|
70
|
-
}
|
|
71
|
-
return schema;
|
|
72
|
-
}
|
|
73
|
-
],
|
|
74
|
-
[
|
|
75
|
-
"record",
|
|
76
|
-
(f) => {
|
|
77
|
-
const keySchema = f.params.keyType ? resolveTypeString(f.params.keyType) : z.string();
|
|
78
|
-
const valueSchema = f.params.valueType ? resolveTypeString(f.params.valueType) : z.unknown();
|
|
79
|
-
return z.record(keySchema, valueSchema);
|
|
80
|
-
}
|
|
81
|
-
],
|
|
82
|
-
[
|
|
83
|
-
"map",
|
|
84
|
-
(f) => {
|
|
85
|
-
const keySchema = f.params.keyType ? resolveTypeString(f.params.keyType) : z.string();
|
|
86
|
-
const valueSchema = f.params.valueType ? resolveTypeString(f.params.valueType) : z.unknown();
|
|
87
|
-
return z.map(keySchema, valueSchema);
|
|
88
|
-
}
|
|
89
|
-
],
|
|
90
|
-
[
|
|
91
|
-
"set",
|
|
92
|
-
(f) => {
|
|
93
|
-
const elementSchema = f.params.elementType ? resolveTypeString(f.params.elementType) : z.unknown();
|
|
94
|
-
return z.set(elementSchema);
|
|
95
|
-
}
|
|
96
|
-
],
|
|
97
|
-
[
|
|
98
|
-
"union",
|
|
99
|
-
(f) => {
|
|
100
|
-
if (f.params.isDiscriminatedUnion && f.params.discriminatedUnion) {
|
|
101
|
-
const { discriminator, cases } = f.params.discriminatedUnion;
|
|
102
|
-
const schemas = Object.values(cases).map(
|
|
103
|
-
(c) => z.object({
|
|
104
|
-
[discriminator]: z.literal(c.value),
|
|
105
|
-
...Object.fromEntries(
|
|
106
|
-
c.fields.map((field) => [field.name, buildFieldSchema(field)])
|
|
107
|
-
)
|
|
108
|
-
})
|
|
109
|
-
);
|
|
110
|
-
return z.discriminatedUnion(
|
|
111
|
-
discriminator,
|
|
112
|
-
schemas
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
const types = (f.params.unionTypes ?? []).map(resolveTypeString);
|
|
116
|
-
return z.union(types);
|
|
117
|
-
}
|
|
118
|
-
],
|
|
119
|
-
[
|
|
120
|
-
"intersection",
|
|
121
|
-
(f) => {
|
|
122
|
-
const types = (f.params.unionTypes ?? []).map(resolveTypeString);
|
|
123
|
-
return z.intersection(types[0] ?? z.unknown(), types[1] ?? z.unknown());
|
|
124
|
-
}
|
|
125
|
-
]
|
|
126
|
-
]);
|
|
127
|
-
var coerceDefaultValue = (value, fieldType) => {
|
|
128
|
-
const coercers = /* @__PURE__ */ new Map([
|
|
129
|
-
["number", (v) => Number(v)],
|
|
130
|
-
["boolean", (v) => v === "true"],
|
|
131
|
-
["bigint", (v) => BigInt(v)]
|
|
132
|
-
]);
|
|
133
|
-
const coercer = coercers.get(fieldType);
|
|
134
|
-
if (coercer) {
|
|
135
|
-
return coercer(value);
|
|
136
|
-
}
|
|
137
|
-
return value;
|
|
138
|
-
};
|
|
139
|
-
var applySizeValidation = (schema, type, value, message) => {
|
|
140
|
-
const num = Number(value);
|
|
141
|
-
if (schema instanceof z.ZodString) {
|
|
142
|
-
const methods = {
|
|
143
|
-
min: (n, m) => schema.min(n, m),
|
|
144
|
-
max: (n, m) => schema.max(n, m),
|
|
145
|
-
length: (n, m) => schema.length(n, m)
|
|
146
|
-
};
|
|
147
|
-
return methods[type]?.(num, message) ?? schema;
|
|
148
|
-
}
|
|
149
|
-
if (schema instanceof z.ZodNumber) {
|
|
150
|
-
const methods = {
|
|
151
|
-
min: (n, m) => schema.min(n, m),
|
|
152
|
-
max: (n, m) => schema.max(n, m)
|
|
153
|
-
};
|
|
154
|
-
return methods[type]?.(num, message) ?? schema;
|
|
155
|
-
}
|
|
156
|
-
if (schema instanceof z.ZodArray) {
|
|
157
|
-
const methods = {
|
|
158
|
-
min: (n) => schema.min(n),
|
|
159
|
-
max: (n) => schema.max(n),
|
|
160
|
-
length: (n) => schema.length(n)
|
|
161
|
-
};
|
|
162
|
-
return methods[type]?.(num) ?? schema;
|
|
163
|
-
}
|
|
164
|
-
return schema;
|
|
165
|
-
};
|
|
166
|
-
var VALIDATION_APPLICATORS = /* @__PURE__ */ new Map([
|
|
167
|
-
// Size validations
|
|
168
|
-
["min", (s, r) => applySizeValidation(s, "min", r.value, r.message)],
|
|
169
|
-
["max", (s, r) => applySizeValidation(s, "max", r.value, r.message)],
|
|
170
|
-
["length", (s, r) => applySizeValidation(s, "length", r.value, r.message)],
|
|
171
|
-
// String validations
|
|
172
|
-
["email", (s, r) => s instanceof z.ZodString ? s.email(r.message) : s],
|
|
173
|
-
["url", (s, r) => s instanceof z.ZodString ? s.url(r.message) : s],
|
|
174
|
-
["uuid", (s, r) => s instanceof z.ZodString ? s.uuid(r.message) : s],
|
|
175
|
-
["cuid", (s, r) => s instanceof z.ZodString ? s.cuid(r.message) : s],
|
|
176
|
-
["cuid2", (s, r) => s instanceof z.ZodString ? s.cuid2(r.message) : s],
|
|
177
|
-
["ulid", (s, r) => s instanceof z.ZodString ? s.ulid(r.message) : s],
|
|
178
|
-
[
|
|
179
|
-
"regex",
|
|
180
|
-
(s, r) => s instanceof z.ZodString ? s.regex(new RegExp(r.value), r.message) : s
|
|
181
|
-
],
|
|
182
|
-
[
|
|
183
|
-
"startsWith",
|
|
184
|
-
(s, r) => s instanceof z.ZodString ? s.startsWith(r.value, r.message) : s
|
|
185
|
-
],
|
|
186
|
-
[
|
|
187
|
-
"endsWith",
|
|
188
|
-
(s, r) => s instanceof z.ZodString ? s.endsWith(r.value, r.message) : s
|
|
189
|
-
],
|
|
190
|
-
[
|
|
191
|
-
"datetime",
|
|
192
|
-
(s, _r, f) => {
|
|
193
|
-
if (!(s instanceof z.ZodString)) {
|
|
194
|
-
return s;
|
|
195
|
-
}
|
|
196
|
-
const opts = f.params.stringOptions?.datetime;
|
|
197
|
-
return s.datetime(opts);
|
|
198
|
-
}
|
|
199
|
-
],
|
|
200
|
-
[
|
|
201
|
-
"ip",
|
|
202
|
-
(s, _r, f) => {
|
|
203
|
-
if (!(s instanceof z.ZodString)) {
|
|
204
|
-
return s;
|
|
205
|
-
}
|
|
206
|
-
const version = f.params.stringOptions?.ip?.version;
|
|
207
|
-
if (version === "v6") {
|
|
208
|
-
return s.ipv6();
|
|
209
|
-
}
|
|
210
|
-
return s.ipv4();
|
|
211
|
-
}
|
|
212
|
-
],
|
|
213
|
-
// String transforms
|
|
214
|
-
["trim", (s) => s instanceof z.ZodString ? s.trim() : s],
|
|
215
|
-
["toLowerCase", (s) => s instanceof z.ZodString ? s.toLowerCase() : s],
|
|
216
|
-
["toUpperCase", (s) => s instanceof z.ZodString ? s.toUpperCase() : s],
|
|
217
|
-
// Number validations
|
|
218
|
-
["int", (s, r) => s instanceof z.ZodNumber ? s.int(r.message) : s],
|
|
219
|
-
[
|
|
220
|
-
"positive",
|
|
221
|
-
(s, r) => s instanceof z.ZodNumber ? s.positive(r.message) : s
|
|
222
|
-
],
|
|
223
|
-
[
|
|
224
|
-
"negative",
|
|
225
|
-
(s, r) => s instanceof z.ZodNumber ? s.negative(r.message) : s
|
|
226
|
-
],
|
|
227
|
-
[
|
|
228
|
-
"multipleOf",
|
|
229
|
-
(s, r) => s instanceof z.ZodNumber ? s.multipleOf(Number(r.value), r.message) : s
|
|
230
|
-
],
|
|
231
|
-
["finite", (s, r) => s instanceof z.ZodNumber ? s.finite(r.message) : s],
|
|
232
|
-
["safe", (s, r) => s instanceof z.ZodNumber ? s.safe(r.message) : s],
|
|
233
|
-
// Collection
|
|
234
|
-
[
|
|
235
|
-
"nonempty",
|
|
236
|
-
(s) => {
|
|
237
|
-
if (s instanceof z.ZodString) {
|
|
238
|
-
return s.min(1);
|
|
239
|
-
}
|
|
240
|
-
if (s instanceof z.ZodArray) {
|
|
241
|
-
return s.nonempty();
|
|
242
|
-
}
|
|
243
|
-
return s;
|
|
244
|
-
}
|
|
245
|
-
],
|
|
246
|
-
// Wrapping modifiers
|
|
247
|
-
["optional", (s) => s.optional()],
|
|
248
|
-
["nullable", (s) => s.nullable()],
|
|
249
|
-
["nullish", (s) => s.nullish()],
|
|
250
|
-
// Default & catch
|
|
251
|
-
["default", (s, r, f) => s.default(coerceDefaultValue(r.value, f.type))],
|
|
252
|
-
["catch", (s, r, f) => s.catch(coerceDefaultValue(r.value, f.type))],
|
|
253
|
-
// Readonly
|
|
254
|
-
["readonly", (s) => s.readonly()]
|
|
255
|
-
]);
|
|
256
|
-
var buildFieldSchema = (field) => {
|
|
257
|
-
const builder = TYPE_BUILDERS.get(field.type);
|
|
258
|
-
if (!builder) {
|
|
259
|
-
return z.unknown();
|
|
260
|
-
}
|
|
261
|
-
let schema = builder(field);
|
|
262
|
-
for (const rule of field.validations) {
|
|
263
|
-
const applicator = VALIDATION_APPLICATORS.get(rule.type);
|
|
264
|
-
if (applicator) {
|
|
265
|
-
schema = applicator(schema, rule, field);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
if (field.params.description) {
|
|
269
|
-
schema = schema.describe(field.params.description);
|
|
270
|
-
}
|
|
271
|
-
return schema;
|
|
272
|
-
};
|
|
273
|
-
var buildZodSchema = (fields) => {
|
|
274
|
-
const shape = {};
|
|
275
|
-
for (const field of fields) {
|
|
276
|
-
shape[field.name] = buildFieldSchema(field);
|
|
277
|
-
}
|
|
278
|
-
return z.object(shape);
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
export {
|
|
282
|
-
buildFieldSchema,
|
|
283
|
-
buildZodSchema
|
|
284
|
-
};
|