pcific 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +50 -2
- package/dist/SKILL.md +127 -0
- package/dist/api/catalog.js +637 -0
- package/dist/api/run.js +960 -0
- package/dist/api/search.js +171 -0
- package/dist/api/shortcuts.js +799 -0
- package/dist/bin.js +648 -0
- package/dist/commands/auth.js +513 -0
- package/dist/commands/doctor.js +410 -0
- package/dist/commands/init.js +306 -0
- package/dist/commands/sync.js +214 -0
- package/dist/detect.js +214 -0
- package/dist/env.js +339 -0
- package/dist/goLive.js +629 -0
- package/dist/keyLink.js +305 -0
- package/dist/mcp/server.js +206 -0
- package/dist/mcp/tools.js +689 -0
- package/dist/python/template.js +793 -0
- package/dist/schema/discover.js +324 -0
- package/dist/schema/helpers.js +396 -0
- package/dist/schema/prismaToJsonSchema.js +197 -0
- package/dist/schema/push.js +570 -0
- package/dist/schema/tsToJsonSchema.js +405 -0
- package/dist/schema/zodToJsonSchema.js +442 -0
- package/dist/templates/openapi.json +1 -0
- package/dist/templates/sdk/auth.ts +139 -0
- package/dist/templates/sdk/client.ts +1059 -0
- package/dist/templates/sdk/errors.ts +76 -0
- package/dist/templates/sdk/generated-types.ts +5746 -0
- package/dist/templates/sdk/http.ts +359 -0
- package/dist/templates/sdk/index.ts +84 -0
- package/dist/templates/sdk/json.ts +24 -0
- package/dist/templates/sdk/kit.ts +516 -0
- package/dist/templates/sdk/multipart.ts +99 -0
- package/dist/templates/sdk/sse.ts +131 -0
- package/dist/templates/sdk/verify.ts +766 -0
- package/dist/templates/sdk-js/auth.d.ts +20 -0
- package/dist/templates/sdk-js/auth.js +85 -0
- package/dist/templates/sdk-js/client.d.ts +324 -0
- package/dist/templates/sdk-js/client.js +607 -0
- package/dist/templates/sdk-js/errors.d.ts +51 -0
- package/dist/templates/sdk-js/errors.js +48 -0
- package/dist/templates/sdk-js/generated-types.d.ts +5736 -0
- package/dist/templates/sdk-js/generated-types.js +5 -0
- package/dist/templates/sdk-js/http.d.ts +50 -0
- package/dist/templates/sdk-js/http.js +263 -0
- package/dist/templates/sdk-js/index.d.ts +9 -0
- package/dist/templates/sdk-js/index.js +7 -0
- package/dist/templates/sdk-js/json.d.ts +11 -0
- package/dist/templates/sdk-js/json.js +16 -0
- package/dist/templates/sdk-js/kit.d.ts +220 -0
- package/dist/templates/sdk-js/kit.js +208 -0
- package/dist/templates/sdk-js/multipart.d.ts +23 -0
- package/dist/templates/sdk-js/multipart.js +61 -0
- package/dist/templates/sdk-js/sse.d.ts +12 -0
- package/dist/templates/sdk-js/sse.js +99 -0
- package/dist/templates/sdk-js/verify.d.ts +114 -0
- package/dist/templates/sdk-js/verify.js +506 -0
- package/dist/templates.js +925 -0
- package/dist/ui.js +399 -0
- package/dist/webhooks/events.js +171 -0
- package/dist/webhooks/listen.js +772 -0
- package/package.json +32 -4
|
@@ -0,0 +1,637 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
const HTTP_METHODS = [
|
|
5
|
+
{ lower: "get", upper: "GET" },
|
|
6
|
+
{ lower: "post", upper: "POST" },
|
|
7
|
+
{ lower: "put", upper: "PUT" },
|
|
8
|
+
{ lower: "delete", upper: "DELETE" },
|
|
9
|
+
{ lower: "patch", upper: "PATCH" },
|
|
10
|
+
];
|
|
11
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
export function isJsonObject(value) {
|
|
13
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
export function isJsonString(value) {
|
|
16
|
+
return typeof value === "string";
|
|
17
|
+
}
|
|
18
|
+
export function isJsonNumber(value) {
|
|
19
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
20
|
+
}
|
|
21
|
+
export function isJsonBoolean(value) {
|
|
22
|
+
return typeof value === "boolean";
|
|
23
|
+
}
|
|
24
|
+
function isStringArray(value) {
|
|
25
|
+
return Array.isArray(value) && value.every((entry) => isJsonString(entry));
|
|
26
|
+
}
|
|
27
|
+
function decodePointerSegment(segment) {
|
|
28
|
+
return segment.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
29
|
+
}
|
|
30
|
+
function lookupLocalRef(spec, ref) {
|
|
31
|
+
if (!ref.startsWith("#/")) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
const parts = ref.slice(2).split("/").map(decodePointerSegment);
|
|
35
|
+
let current = spec;
|
|
36
|
+
for (const part of parts) {
|
|
37
|
+
if (!isJsonObject(current) || !(part in current)) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
current = current[part];
|
|
41
|
+
}
|
|
42
|
+
return current;
|
|
43
|
+
}
|
|
44
|
+
function applyKnownSchemaFields(spec, raw, seen, depth) {
|
|
45
|
+
const out = {};
|
|
46
|
+
if (isJsonString(raw.type) || isStringArray(raw.type)) {
|
|
47
|
+
out.type = raw.type;
|
|
48
|
+
}
|
|
49
|
+
if (isJsonString(raw.description)) {
|
|
50
|
+
out.description = raw.description;
|
|
51
|
+
}
|
|
52
|
+
if (Array.isArray(raw.enum)) {
|
|
53
|
+
out.enum = raw.enum;
|
|
54
|
+
}
|
|
55
|
+
if (raw.const !== undefined) {
|
|
56
|
+
out.const = raw.const;
|
|
57
|
+
}
|
|
58
|
+
if (raw.default !== undefined) {
|
|
59
|
+
out.default = raw.default;
|
|
60
|
+
}
|
|
61
|
+
if (raw.example !== undefined) {
|
|
62
|
+
out.example = raw.example;
|
|
63
|
+
}
|
|
64
|
+
if (Array.isArray(raw.examples)) {
|
|
65
|
+
out.examples = raw.examples;
|
|
66
|
+
}
|
|
67
|
+
if (isJsonString(raw.format)) {
|
|
68
|
+
out.format = raw.format;
|
|
69
|
+
}
|
|
70
|
+
if (isStringArray(raw.required)) {
|
|
71
|
+
out.required = raw.required;
|
|
72
|
+
}
|
|
73
|
+
if (isJsonNumber(raw.minLength)) {
|
|
74
|
+
out.minLength = raw.minLength;
|
|
75
|
+
}
|
|
76
|
+
if (isJsonNumber(raw.maxLength)) {
|
|
77
|
+
out.maxLength = raw.maxLength;
|
|
78
|
+
}
|
|
79
|
+
if (isJsonNumber(raw.minimum)) {
|
|
80
|
+
out.minimum = raw.minimum;
|
|
81
|
+
}
|
|
82
|
+
if (isJsonNumber(raw.maximum)) {
|
|
83
|
+
out.maximum = raw.maximum;
|
|
84
|
+
}
|
|
85
|
+
if (isJsonNumber(raw.minItems)) {
|
|
86
|
+
out.minItems = raw.minItems;
|
|
87
|
+
}
|
|
88
|
+
if (isJsonNumber(raw.maxItems)) {
|
|
89
|
+
out.maxItems = raw.maxItems;
|
|
90
|
+
}
|
|
91
|
+
if (isJsonObject(raw.properties)) {
|
|
92
|
+
const resolvedProps = {};
|
|
93
|
+
for (const [propName, propSchema] of Object.entries(raw.properties)) {
|
|
94
|
+
const resolvedProp = resolveJsonSchemaRef(spec, propSchema, seen, depth + 1);
|
|
95
|
+
if (resolvedProp) {
|
|
96
|
+
resolvedProps[propName] = resolvedProp;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
out.properties = resolvedProps;
|
|
100
|
+
}
|
|
101
|
+
if (isJsonObject(raw.items)) {
|
|
102
|
+
out.items = resolveJsonSchemaRef(spec, raw.items, seen, depth + 1);
|
|
103
|
+
}
|
|
104
|
+
if (isJsonBoolean(raw.additionalProperties)) {
|
|
105
|
+
out.additionalProperties = raw.additionalProperties;
|
|
106
|
+
}
|
|
107
|
+
else if (isJsonObject(raw.additionalProperties)) {
|
|
108
|
+
out.additionalProperties = resolveJsonSchemaRef(spec, raw.additionalProperties, seen, depth + 1);
|
|
109
|
+
}
|
|
110
|
+
if (Array.isArray(raw.oneOf)) {
|
|
111
|
+
out.oneOf = raw.oneOf
|
|
112
|
+
.map((entry) => resolveJsonSchemaRef(spec, entry, seen, depth + 1))
|
|
113
|
+
.filter((entry) => entry !== undefined);
|
|
114
|
+
}
|
|
115
|
+
if (Array.isArray(raw.anyOf)) {
|
|
116
|
+
out.anyOf = raw.anyOf
|
|
117
|
+
.map((entry) => resolveJsonSchemaRef(spec, entry, seen, depth + 1))
|
|
118
|
+
.filter((entry) => entry !== undefined);
|
|
119
|
+
}
|
|
120
|
+
if (Array.isArray(raw.allOf)) {
|
|
121
|
+
out.allOf = raw.allOf
|
|
122
|
+
.map((entry) => resolveJsonSchemaRef(spec, entry, seen, depth + 1))
|
|
123
|
+
.filter((entry) => entry !== undefined);
|
|
124
|
+
}
|
|
125
|
+
if (Array.isArray(out.allOf) && out.allOf.length > 0 && !out.properties) {
|
|
126
|
+
const mergedProps = {};
|
|
127
|
+
const mergedRequired = new Set(out.required ?? []);
|
|
128
|
+
for (const branch of out.allOf) {
|
|
129
|
+
if (branch.properties) {
|
|
130
|
+
Object.assign(mergedProps, branch.properties);
|
|
131
|
+
}
|
|
132
|
+
if (branch.required) {
|
|
133
|
+
for (const req of branch.required) {
|
|
134
|
+
mergedRequired.add(req);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (Object.keys(mergedProps).length > 0) {
|
|
139
|
+
out.type = out.type ?? "object";
|
|
140
|
+
out.properties = mergedProps;
|
|
141
|
+
if (mergedRequired.size > 0) {
|
|
142
|
+
out.required = [...mergedRequired];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
export function resolveJsonSchemaRef(spec, schema, seen = new Set(), depth = 0) {
|
|
149
|
+
if (!isJsonObject(schema)) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
if (depth > 24) {
|
|
153
|
+
return { type: "object", description: "Nested schema truncated for display." };
|
|
154
|
+
}
|
|
155
|
+
if (isJsonString(schema.$ref)) {
|
|
156
|
+
const ref = schema.$ref;
|
|
157
|
+
if (seen.has(ref)) {
|
|
158
|
+
return {
|
|
159
|
+
type: "object",
|
|
160
|
+
$circularRef: ref,
|
|
161
|
+
description: isJsonString(schema.description)
|
|
162
|
+
? schema.description
|
|
163
|
+
: `Circular reference to ${ref}.`,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
const target = lookupLocalRef(spec, ref);
|
|
167
|
+
if (!isJsonObject(target)) {
|
|
168
|
+
const unresolved = { type: "object", $ref: ref };
|
|
169
|
+
if (isJsonString(schema.description)) {
|
|
170
|
+
unresolved.description = schema.description;
|
|
171
|
+
}
|
|
172
|
+
return unresolved;
|
|
173
|
+
}
|
|
174
|
+
const nextSeen = new Set(seen);
|
|
175
|
+
nextSeen.add(ref);
|
|
176
|
+
const resolvedTarget = resolveJsonSchemaRef(spec, target, nextSeen, depth + 1) ?? {};
|
|
177
|
+
const siblings = applyKnownSchemaFields(spec, schema, nextSeen, depth + 1);
|
|
178
|
+
return {
|
|
179
|
+
...resolvedTarget,
|
|
180
|
+
...siblings,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
return applyKnownSchemaFields(spec, schema, seen, depth);
|
|
184
|
+
}
|
|
185
|
+
function resolveParameter(spec, raw) {
|
|
186
|
+
if (!isJsonObject(raw)) {
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
let param = raw;
|
|
190
|
+
if (isJsonString(raw.$ref)) {
|
|
191
|
+
const lookedUp = lookupLocalRef(spec, raw.$ref);
|
|
192
|
+
if (!isJsonObject(lookedUp)) {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
param = { ...lookedUp, ...raw };
|
|
196
|
+
}
|
|
197
|
+
const name = isJsonString(param.name) ? param.name : "";
|
|
198
|
+
const location = param.in;
|
|
199
|
+
if (!name ||
|
|
200
|
+
(location !== "path" && location !== "query" && location !== "header" && location !== "cookie")) {
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
name,
|
|
205
|
+
in: location,
|
|
206
|
+
required: location === "path" ? true : Boolean(param.required),
|
|
207
|
+
description: isJsonString(param.description) ? param.description : "",
|
|
208
|
+
schema: resolveJsonSchemaRef(spec, param.schema),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
function collectPropertyNames(schema, into, depth = 0) {
|
|
212
|
+
if (!schema || depth > 5) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (schema.properties) {
|
|
216
|
+
for (const [key, propSchema] of Object.entries(schema.properties)) {
|
|
217
|
+
into.add(key);
|
|
218
|
+
collectPropertyNames(propSchema, into, depth + 1);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (schema.items) {
|
|
222
|
+
collectPropertyNames(schema.items, into, depth + 1);
|
|
223
|
+
}
|
|
224
|
+
for (const branchKey of ["oneOf", "anyOf", "allOf"]) {
|
|
225
|
+
const branches = schema[branchKey];
|
|
226
|
+
if (Array.isArray(branches)) {
|
|
227
|
+
for (const branch of branches) {
|
|
228
|
+
collectPropertyNames(branch, into, depth + 1);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function primarySchemaType(schema) {
|
|
234
|
+
if (isJsonString(schema.type)) {
|
|
235
|
+
return schema.type;
|
|
236
|
+
}
|
|
237
|
+
if (Array.isArray(schema.type)) {
|
|
238
|
+
return schema.type.find((t) => t !== "null") ?? schema.type[0];
|
|
239
|
+
}
|
|
240
|
+
if (schema.properties) {
|
|
241
|
+
return "object";
|
|
242
|
+
}
|
|
243
|
+
if (schema.items) {
|
|
244
|
+
return "array";
|
|
245
|
+
}
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
function buildExampleValue(schema, depth = 0) {
|
|
249
|
+
if (!schema || depth > 4 || schema.$circularRef) {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
if (schema.example !== undefined) {
|
|
253
|
+
return schema.example;
|
|
254
|
+
}
|
|
255
|
+
if (Array.isArray(schema.examples) && schema.examples.length > 0) {
|
|
256
|
+
return schema.examples[0];
|
|
257
|
+
}
|
|
258
|
+
if (schema.const !== undefined) {
|
|
259
|
+
return schema.const;
|
|
260
|
+
}
|
|
261
|
+
if (schema.default !== undefined) {
|
|
262
|
+
return schema.default;
|
|
263
|
+
}
|
|
264
|
+
if (Array.isArray(schema.enum) && schema.enum.length > 0) {
|
|
265
|
+
return schema.enum[0];
|
|
266
|
+
}
|
|
267
|
+
if (Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
|
|
268
|
+
return buildExampleValue(schema.oneOf[0], depth + 1);
|
|
269
|
+
}
|
|
270
|
+
if (Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
|
|
271
|
+
return buildExampleValue(schema.anyOf[0], depth + 1);
|
|
272
|
+
}
|
|
273
|
+
const type = primarySchemaType(schema);
|
|
274
|
+
if (type === "object") {
|
|
275
|
+
const out = {};
|
|
276
|
+
const props = schema.properties ?? {};
|
|
277
|
+
const requiredSet = new Set(schema.required ?? []);
|
|
278
|
+
const keys = Object.keys(props);
|
|
279
|
+
const orderedKeys = [
|
|
280
|
+
...keys.filter((k) => requiredSet.has(k)),
|
|
281
|
+
...keys.filter((k) => !requiredSet.has(k)),
|
|
282
|
+
];
|
|
283
|
+
const selectedKeys = requiredSet.size > 0
|
|
284
|
+
? orderedKeys.slice(0, Math.max(requiredSet.size, 4))
|
|
285
|
+
: orderedKeys.slice(0, 4);
|
|
286
|
+
for (const key of selectedKeys) {
|
|
287
|
+
const value = buildExampleValue(props[key], depth + 1);
|
|
288
|
+
if (value !== undefined) {
|
|
289
|
+
out[key] = value;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return out;
|
|
293
|
+
}
|
|
294
|
+
if (type === "array") {
|
|
295
|
+
const itemExample = buildExampleValue(schema.items, depth + 1);
|
|
296
|
+
return itemExample !== undefined ? [itemExample] : [];
|
|
297
|
+
}
|
|
298
|
+
if (type === "boolean") {
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
if (type === "integer" || type === "number") {
|
|
302
|
+
if (isJsonNumber(schema.minimum)) {
|
|
303
|
+
return schema.minimum;
|
|
304
|
+
}
|
|
305
|
+
return type === "integer" ? 1 : 0.5;
|
|
306
|
+
}
|
|
307
|
+
if (type === "string") {
|
|
308
|
+
if (schema.format === "uri" || schema.format === "url") {
|
|
309
|
+
return "https://example.com";
|
|
310
|
+
}
|
|
311
|
+
if (schema.format === "date") {
|
|
312
|
+
return "2026-01-01";
|
|
313
|
+
}
|
|
314
|
+
if (schema.format === "date-time") {
|
|
315
|
+
return "2026-01-01T00:00:00Z";
|
|
316
|
+
}
|
|
317
|
+
return "example";
|
|
318
|
+
}
|
|
319
|
+
if (type === "null") {
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
return undefined;
|
|
323
|
+
}
|
|
324
|
+
export function resolveOpenApiSpecPath(overridePath) {
|
|
325
|
+
const candidates = [
|
|
326
|
+
overridePath,
|
|
327
|
+
path.resolve(__dirname, "../templates/openapi.json"),
|
|
328
|
+
path.resolve(__dirname, "../../templates/openapi.json"),
|
|
329
|
+
path.resolve(__dirname, "../../../openapi/openapi.json"),
|
|
330
|
+
path.resolve(__dirname, "../../openapi/openapi.json"),
|
|
331
|
+
path.resolve(process.cwd(), "packages/pcific-kit/openapi/openapi.json"),
|
|
332
|
+
].filter((candidate) => Boolean(candidate));
|
|
333
|
+
for (const candidate of candidates) {
|
|
334
|
+
if (existsSync(candidate)) {
|
|
335
|
+
return candidate;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
throw new Error(`Could not find PCIFIC OpenAPI spec. Checked: ${candidates.join(", ")}. Pass openapiPath or rebuild templates.`);
|
|
339
|
+
}
|
|
340
|
+
export function loadOpenApiCatalog(options = {}) {
|
|
341
|
+
let specPath = options.openapiPath ?? "<inline>";
|
|
342
|
+
let spec;
|
|
343
|
+
if (options.spec) {
|
|
344
|
+
spec = options.spec;
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
specPath = resolveOpenApiSpecPath(options.openapiPath);
|
|
348
|
+
const raw = readFileSync(specPath, "utf8");
|
|
349
|
+
// SAFETY: JSON.parse returns JSON values matching ApiJsonValue.
|
|
350
|
+
const parsed = JSON.parse(raw);
|
|
351
|
+
if (!isJsonObject(parsed)) {
|
|
352
|
+
throw new Error(`Invalid OpenAPI document at ${specPath}: expected a JSON object.`);
|
|
353
|
+
}
|
|
354
|
+
spec = parsed;
|
|
355
|
+
}
|
|
356
|
+
const info = isJsonObject(spec.info) ? spec.info : {};
|
|
357
|
+
const title = isJsonString(info.title) ? info.title : "PCIFIC API";
|
|
358
|
+
const version = isJsonString(info.version) ? info.version : "1";
|
|
359
|
+
const servers = Array.isArray(spec.servers) ? spec.servers : [];
|
|
360
|
+
let serverUrl = "https://business.pcific.ai";
|
|
361
|
+
for (const entry of servers) {
|
|
362
|
+
if (isJsonObject(entry) && isJsonString(entry.url)) {
|
|
363
|
+
serverUrl = entry.url;
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
const paths = isJsonObject(spec.paths) ? spec.paths : {};
|
|
368
|
+
const operations = [];
|
|
369
|
+
const byOperationId = new Map();
|
|
370
|
+
const byMethodPath = new Map();
|
|
371
|
+
const tagSet = new Set();
|
|
372
|
+
for (const [routePath, pathItem] of Object.entries(paths)) {
|
|
373
|
+
if (!isJsonObject(pathItem)) {
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
const pathLevelParams = Array.isArray(pathItem.parameters) ? pathItem.parameters : [];
|
|
377
|
+
for (const methodEntry of HTTP_METHODS) {
|
|
378
|
+
const rawOp = pathItem[methodEntry.lower];
|
|
379
|
+
if (!isJsonObject(rawOp)) {
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
const method = methodEntry.upper;
|
|
383
|
+
const operationId = isJsonString(rawOp.operationId) && rawOp.operationId.trim()
|
|
384
|
+
? rawOp.operationId.trim()
|
|
385
|
+
: `${methodEntry.lower}-${routePath.replace(/[^a-zA-Z0-9]+/g, "-").replace(/^-|-$/g, "")}`;
|
|
386
|
+
const rawParams = [
|
|
387
|
+
...pathLevelParams,
|
|
388
|
+
...(Array.isArray(rawOp.parameters) ? rawOp.parameters : []),
|
|
389
|
+
];
|
|
390
|
+
const paramMap = new Map();
|
|
391
|
+
for (const rawParam of rawParams) {
|
|
392
|
+
const resolved = resolveParameter(spec, rawParam);
|
|
393
|
+
if (resolved) {
|
|
394
|
+
paramMap.set(`${resolved.in}:${resolved.name}`, resolved);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
const parameters = [...paramMap.values()];
|
|
398
|
+
let requestBodyObj;
|
|
399
|
+
if (isJsonObject(rawOp.requestBody)) {
|
|
400
|
+
if (isJsonString(rawOp.requestBody.$ref)) {
|
|
401
|
+
const lookedUp = lookupLocalRef(spec, rawOp.requestBody.$ref);
|
|
402
|
+
if (isJsonObject(lookedUp)) {
|
|
403
|
+
requestBodyObj = lookedUp;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
requestBodyObj = rawOp.requestBody;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
const content = requestBodyObj && isJsonObject(requestBodyObj.content) ? requestBodyObj.content : undefined;
|
|
411
|
+
const requestBodyContentTypes = content ? Object.keys(content) : [];
|
|
412
|
+
const requestBodyRequired = Boolean(requestBodyObj?.required);
|
|
413
|
+
let requestBodySchema;
|
|
414
|
+
if (content) {
|
|
415
|
+
const firstType = requestBodyContentTypes[0];
|
|
416
|
+
const jsonEntry = content["application/json"];
|
|
417
|
+
const fallbackEntry = firstType ? content[firstType] : undefined;
|
|
418
|
+
const preferredContent = isJsonObject(jsonEntry)
|
|
419
|
+
? jsonEntry
|
|
420
|
+
: isJsonObject(fallbackEntry)
|
|
421
|
+
? fallbackEntry
|
|
422
|
+
: undefined;
|
|
423
|
+
if (preferredContent && isJsonObject(preferredContent.schema)) {
|
|
424
|
+
requestBodySchema = resolveJsonSchemaRef(spec, preferredContent.schema);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
const bodyPropertyNameSet = new Set();
|
|
428
|
+
collectPropertyNames(requestBodySchema, bodyPropertyNameSet);
|
|
429
|
+
const tags = Array.isArray(rawOp.tags)
|
|
430
|
+
? rawOp.tags.filter((t) => isJsonString(t))
|
|
431
|
+
: [];
|
|
432
|
+
for (const tag of tags) {
|
|
433
|
+
tagSet.add(tag);
|
|
434
|
+
}
|
|
435
|
+
const hasIdempotencyHeader = parameters.some((p) => p.in === "header" && p.name.toLowerCase() === "idempotency-key");
|
|
436
|
+
const idempotent = hasIdempotencyHeader || method !== "GET";
|
|
437
|
+
const operation = {
|
|
438
|
+
operationId,
|
|
439
|
+
method,
|
|
440
|
+
path: routePath,
|
|
441
|
+
summary: isJsonString(rawOp.summary) ? rawOp.summary : "",
|
|
442
|
+
description: isJsonString(rawOp.description) ? rawOp.description : "",
|
|
443
|
+
tags,
|
|
444
|
+
parameters,
|
|
445
|
+
requestBodyContentTypes,
|
|
446
|
+
requestBodyRequired,
|
|
447
|
+
requestBodySchema,
|
|
448
|
+
exampleBody: requestBodySchema ? buildExampleValue(requestBodySchema) : undefined,
|
|
449
|
+
idempotent,
|
|
450
|
+
bodyPropertyNames: [...bodyPropertyNameSet],
|
|
451
|
+
};
|
|
452
|
+
operations.push(operation);
|
|
453
|
+
byOperationId.set(operationId.toLowerCase(), operation);
|
|
454
|
+
byMethodPath.set(`${method} ${routePath.toLowerCase()}`, operation);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return {
|
|
458
|
+
title,
|
|
459
|
+
version,
|
|
460
|
+
serverUrl,
|
|
461
|
+
specPath,
|
|
462
|
+
tags: [...tagSet],
|
|
463
|
+
operations,
|
|
464
|
+
byOperationId,
|
|
465
|
+
byMethodPath,
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
export function findOperation(catalog, idOrMethodPath) {
|
|
469
|
+
const trimmed = idOrMethodPath.trim();
|
|
470
|
+
if (!trimmed) {
|
|
471
|
+
return undefined;
|
|
472
|
+
}
|
|
473
|
+
const direct = catalog.byOperationId.get(trimmed.toLowerCase());
|
|
474
|
+
if (direct) {
|
|
475
|
+
return direct;
|
|
476
|
+
}
|
|
477
|
+
const methodPathMatch = /^(GET|POST|PUT|DELETE|PATCH)\s+(.+)$/i.exec(trimmed);
|
|
478
|
+
if (methodPathMatch) {
|
|
479
|
+
const method = methodPathMatch[1].toUpperCase();
|
|
480
|
+
let routePath = methodPathMatch[2].trim().toLowerCase();
|
|
481
|
+
if (!routePath.startsWith("/")) {
|
|
482
|
+
routePath = `/${routePath}`;
|
|
483
|
+
}
|
|
484
|
+
const exact = catalog.byMethodPath.get(`${method} ${routePath}`);
|
|
485
|
+
if (exact) {
|
|
486
|
+
return exact;
|
|
487
|
+
}
|
|
488
|
+
if (!routePath.startsWith("/api/v1/") && !routePath.startsWith("/api/")) {
|
|
489
|
+
const prefixed = catalog.byMethodPath.get(`${method} /api/v1${routePath}`);
|
|
490
|
+
if (prefixed) {
|
|
491
|
+
return prefixed;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
const normalizedPath = trimmed.startsWith("/")
|
|
496
|
+
? trimmed.toLowerCase()
|
|
497
|
+
: `/${trimmed.toLowerCase()}`;
|
|
498
|
+
const pathMatches = catalog.operations.filter((op) => op.path.toLowerCase() === normalizedPath ||
|
|
499
|
+
op.path.toLowerCase() === `/api/v1${normalizedPath}`);
|
|
500
|
+
if (pathMatches.length === 1) {
|
|
501
|
+
return pathMatches[0];
|
|
502
|
+
}
|
|
503
|
+
return undefined;
|
|
504
|
+
}
|
|
505
|
+
function tokenize(input) {
|
|
506
|
+
return input
|
|
507
|
+
.toLowerCase()
|
|
508
|
+
.split(/[^a-z0-9]+/)
|
|
509
|
+
.filter((token) => token.length > 0);
|
|
510
|
+
}
|
|
511
|
+
export function searchOperations(catalog, query, limit = 10) {
|
|
512
|
+
const trimmed = query.trim();
|
|
513
|
+
const maxResults = Math.max(1, limit);
|
|
514
|
+
if (!trimmed) {
|
|
515
|
+
return catalog.operations.slice(0, maxResults).map((operation) => ({
|
|
516
|
+
operation,
|
|
517
|
+
score: 1,
|
|
518
|
+
matchedIn: [],
|
|
519
|
+
}));
|
|
520
|
+
}
|
|
521
|
+
const normalizedQuery = trimmed.toLowerCase();
|
|
522
|
+
const tokens = tokenize(trimmed);
|
|
523
|
+
if (tokens.length === 0) {
|
|
524
|
+
return [];
|
|
525
|
+
}
|
|
526
|
+
const results = [];
|
|
527
|
+
for (const op of catalog.operations) {
|
|
528
|
+
const idLower = op.operationId.toLowerCase();
|
|
529
|
+
const idTokens = tokenize(op.operationId);
|
|
530
|
+
const pathLower = op.path.toLowerCase();
|
|
531
|
+
const pathTokens = tokenize(op.path);
|
|
532
|
+
const summaryLower = op.summary.toLowerCase();
|
|
533
|
+
const descLower = op.description.toLowerCase();
|
|
534
|
+
const tagsLower = op.tags.map((t) => t.toLowerCase());
|
|
535
|
+
const bodyPropsLower = op.bodyPropertyNames.map((p) => p.toLowerCase());
|
|
536
|
+
let score = 0;
|
|
537
|
+
const matchedInSet = new Set();
|
|
538
|
+
let matchedTokenCount = 0;
|
|
539
|
+
if (idLower === normalizedQuery) {
|
|
540
|
+
score += 120;
|
|
541
|
+
matchedInSet.add("operationId");
|
|
542
|
+
}
|
|
543
|
+
else if (idLower.includes(normalizedQuery)) {
|
|
544
|
+
score += 55;
|
|
545
|
+
matchedInSet.add("operationId");
|
|
546
|
+
}
|
|
547
|
+
const hyphenPhrase = tokens.join("-");
|
|
548
|
+
if (tokens.length > 1 && idLower.includes(hyphenPhrase)) {
|
|
549
|
+
score += 50;
|
|
550
|
+
matchedInSet.add("operationId");
|
|
551
|
+
}
|
|
552
|
+
if (tokens.length > 1 && pathLower.includes(tokens.join("/"))) {
|
|
553
|
+
score += 45;
|
|
554
|
+
matchedInSet.add("path");
|
|
555
|
+
}
|
|
556
|
+
if (tokens.length > 1 && pathLower.includes(hyphenPhrase)) {
|
|
557
|
+
score += 45;
|
|
558
|
+
matchedInSet.add("path");
|
|
559
|
+
}
|
|
560
|
+
if (tokens.length > 1 && summaryLower.includes(normalizedQuery)) {
|
|
561
|
+
score += 30;
|
|
562
|
+
matchedInSet.add("summary");
|
|
563
|
+
}
|
|
564
|
+
if (tokens.length > 1 &&
|
|
565
|
+
tagsLower.some((tag) => tag === normalizedQuery || tag.includes(normalizedQuery))) {
|
|
566
|
+
score += 28;
|
|
567
|
+
matchedInSet.add("tags");
|
|
568
|
+
}
|
|
569
|
+
for (const token of tokens) {
|
|
570
|
+
let tokenMatched = false;
|
|
571
|
+
if (idTokens.includes(token)) {
|
|
572
|
+
score += 24;
|
|
573
|
+
matchedInSet.add("operationId");
|
|
574
|
+
tokenMatched = true;
|
|
575
|
+
}
|
|
576
|
+
else if (idLower.includes(token)) {
|
|
577
|
+
score += 14;
|
|
578
|
+
matchedInSet.add("operationId");
|
|
579
|
+
tokenMatched = true;
|
|
580
|
+
}
|
|
581
|
+
if (pathTokens.includes(token)) {
|
|
582
|
+
score += 20;
|
|
583
|
+
matchedInSet.add("path");
|
|
584
|
+
tokenMatched = true;
|
|
585
|
+
}
|
|
586
|
+
else if (pathLower.includes(token)) {
|
|
587
|
+
score += 11;
|
|
588
|
+
matchedInSet.add("path");
|
|
589
|
+
tokenMatched = true;
|
|
590
|
+
}
|
|
591
|
+
if (tagsLower.some((tag) => tokenize(tag).includes(token))) {
|
|
592
|
+
score += 12;
|
|
593
|
+
matchedInSet.add("tags");
|
|
594
|
+
tokenMatched = true;
|
|
595
|
+
}
|
|
596
|
+
if (summaryLower.includes(token)) {
|
|
597
|
+
score += 9;
|
|
598
|
+
matchedInSet.add("summary");
|
|
599
|
+
tokenMatched = true;
|
|
600
|
+
}
|
|
601
|
+
if (bodyPropsLower.some((prop) => prop === token || prop.includes(token))) {
|
|
602
|
+
score += 8;
|
|
603
|
+
matchedInSet.add("body");
|
|
604
|
+
tokenMatched = true;
|
|
605
|
+
}
|
|
606
|
+
if (descLower.includes(token)) {
|
|
607
|
+
score += 4;
|
|
608
|
+
matchedInSet.add("description");
|
|
609
|
+
tokenMatched = true;
|
|
610
|
+
}
|
|
611
|
+
if (tokenMatched) {
|
|
612
|
+
matchedTokenCount += 1;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
if (score === 0) {
|
|
616
|
+
continue;
|
|
617
|
+
}
|
|
618
|
+
if (matchedTokenCount === tokens.length) {
|
|
619
|
+
score += 35 * tokens.length;
|
|
620
|
+
}
|
|
621
|
+
else if (tokens.length > 1) {
|
|
622
|
+
score = Math.round(score * (matchedTokenCount / tokens.length));
|
|
623
|
+
}
|
|
624
|
+
results.push({
|
|
625
|
+
operation: op,
|
|
626
|
+
score,
|
|
627
|
+
matchedIn: [...matchedInSet],
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
results.sort((a, b) => {
|
|
631
|
+
if (b.score !== a.score) {
|
|
632
|
+
return b.score - a.score;
|
|
633
|
+
}
|
|
634
|
+
return a.operation.operationId.localeCompare(b.operation.operationId);
|
|
635
|
+
});
|
|
636
|
+
return results.slice(0, maxResults);
|
|
637
|
+
}
|