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
package/dist/ui.js
ADDED
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { emitKeypressEvents } from "node:readline";
|
|
2
|
+
const SECRET_PATTERN = /\bpcific_(test|live)_(?!safeguard\b|orbit\b|ask\b|message_check\b)[A-Za-z0-9_-]{6,}/g;
|
|
3
|
+
const WEBHOOK_SECRET_PATTERN = /whsec_[A-Za-z0-9_-]+/g;
|
|
4
|
+
const BRAILLE_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
5
|
+
const ANSI = {
|
|
6
|
+
reset: "\x1b[0m",
|
|
7
|
+
bold: "\x1b[1m",
|
|
8
|
+
dim: "\x1b[2m",
|
|
9
|
+
cyan: "\x1b[36m",
|
|
10
|
+
blue: "\x1b[34m",
|
|
11
|
+
green: "\x1b[32m",
|
|
12
|
+
yellow: "\x1b[33m",
|
|
13
|
+
red: "\x1b[31m",
|
|
14
|
+
hideCursor: "\x1b[?25l",
|
|
15
|
+
showCursor: "\x1b[?25h",
|
|
16
|
+
clearLine: "\x1b[2K\r",
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Redacts any PCIFIC test or live API key and webhook signing secret anywhere
|
|
20
|
+
* in a string so secrets can never leak to terminal output or JSON logs.
|
|
21
|
+
*/
|
|
22
|
+
export function redactSecrets(text) {
|
|
23
|
+
return text
|
|
24
|
+
.replace(SECRET_PATTERN, (_match, mode) => `pcific_${mode}_[REDACTED]`)
|
|
25
|
+
.replace(WEBHOOK_SECRET_PATTERN, "whsec_[REDACTED]");
|
|
26
|
+
}
|
|
27
|
+
function isStringValue(value) {
|
|
28
|
+
return typeof value === "string";
|
|
29
|
+
}
|
|
30
|
+
function isJsonObjectValue(value) {
|
|
31
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
32
|
+
}
|
|
33
|
+
function redactJsonValue(value) {
|
|
34
|
+
if (isStringValue(value)) {
|
|
35
|
+
return redactSecrets(value);
|
|
36
|
+
}
|
|
37
|
+
if (Array.isArray(value)) {
|
|
38
|
+
return value.map(redactJsonValue);
|
|
39
|
+
}
|
|
40
|
+
if (isJsonObjectValue(value)) {
|
|
41
|
+
const clean = {};
|
|
42
|
+
for (const [key, item] of Object.entries(value)) {
|
|
43
|
+
clean[key] = redactJsonValue(item);
|
|
44
|
+
}
|
|
45
|
+
return clean;
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
function isTruthyEnv(value) {
|
|
50
|
+
if (!value)
|
|
51
|
+
return false;
|
|
52
|
+
const trimmed = value.trim().toLowerCase();
|
|
53
|
+
return trimmed.length > 0 && trimmed !== "0" && trimmed !== "false";
|
|
54
|
+
}
|
|
55
|
+
const ESC = String.fromCharCode(0x1b);
|
|
56
|
+
const ANSI_PATTERN = new RegExp(`${ESC}\\[[0-9;]*[a-zA-Z]`, "g");
|
|
57
|
+
function stripAnsi(text) {
|
|
58
|
+
return text.replace(ANSI_PATTERN, "");
|
|
59
|
+
}
|
|
60
|
+
function formatElapsed(ms) {
|
|
61
|
+
const seconds = Math.max(0, Math.floor(ms / 1000));
|
|
62
|
+
return `${seconds}s`;
|
|
63
|
+
}
|
|
64
|
+
export function createUi(options = {}) {
|
|
65
|
+
const env = options.env ?? process.env;
|
|
66
|
+
const stdin = options.stdin ?? process.stdin;
|
|
67
|
+
const stdout = options.stdout ?? process.stdout;
|
|
68
|
+
const stderr = options.stderr ?? process.stderr;
|
|
69
|
+
const now = options.now ?? (() => Date.now());
|
|
70
|
+
const json = Boolean(options.json);
|
|
71
|
+
const plain = Boolean(options.plain) ||
|
|
72
|
+
json ||
|
|
73
|
+
!stdout.isTTY ||
|
|
74
|
+
isTruthyEnv(env.CI) ||
|
|
75
|
+
isTruthyEnv(env.NO_COLOR) ||
|
|
76
|
+
env.TERM === "dumb";
|
|
77
|
+
const interactive = !plain && Boolean(stdin.isTTY) && Boolean(stdout.isTTY);
|
|
78
|
+
const paint = (code, text) => plain ? text : `${code}${text}${ANSI.reset}`;
|
|
79
|
+
const writeOut = (text) => {
|
|
80
|
+
if (json)
|
|
81
|
+
return;
|
|
82
|
+
stdout.write(redactSecrets(text));
|
|
83
|
+
};
|
|
84
|
+
const writeErr = (text) => {
|
|
85
|
+
if (json)
|
|
86
|
+
return;
|
|
87
|
+
stderr.write(redactSecrets(text));
|
|
88
|
+
};
|
|
89
|
+
const header = (title, subtitle) => {
|
|
90
|
+
if (json)
|
|
91
|
+
return;
|
|
92
|
+
if (plain) {
|
|
93
|
+
writeOut(`[pcific] ${title}${subtitle ? ` - ${subtitle}` : ""}\n`);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
writeOut(`\n ${paint(ANSI.bold + ANSI.cyan, "PCIFIC")} ${paint(ANSI.bold, title)}\n`);
|
|
97
|
+
if (subtitle) {
|
|
98
|
+
writeOut(` ${paint(ANSI.dim, subtitle)}\n`);
|
|
99
|
+
}
|
|
100
|
+
writeOut("\n");
|
|
101
|
+
};
|
|
102
|
+
const step = (message) => {
|
|
103
|
+
if (json)
|
|
104
|
+
return;
|
|
105
|
+
if (plain) {
|
|
106
|
+
writeOut(`[step] ${message}\n`);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
writeOut(` ${paint(ANSI.cyan, "→")} ${message}\n`);
|
|
110
|
+
};
|
|
111
|
+
const info = (message) => {
|
|
112
|
+
if (json)
|
|
113
|
+
return;
|
|
114
|
+
if (plain) {
|
|
115
|
+
writeOut(`[info] ${message}\n`);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
writeOut(` ${paint(ANSI.dim, message)}\n`);
|
|
119
|
+
};
|
|
120
|
+
const ok = (message) => {
|
|
121
|
+
if (json)
|
|
122
|
+
return;
|
|
123
|
+
if (plain) {
|
|
124
|
+
writeOut(`[ok] ${message}\n`);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
writeOut(` ${paint(ANSI.green, "✓")} ${message}\n`);
|
|
128
|
+
};
|
|
129
|
+
const warn = (message) => {
|
|
130
|
+
if (json)
|
|
131
|
+
return;
|
|
132
|
+
if (plain) {
|
|
133
|
+
writeOut(`[warn] ${message}\n`);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
writeOut(` ${paint(ANSI.yellow, "!")} ${message}\n`);
|
|
137
|
+
};
|
|
138
|
+
const error = (message, tryThis) => {
|
|
139
|
+
if (json)
|
|
140
|
+
return;
|
|
141
|
+
if (plain) {
|
|
142
|
+
writeErr(`[error] ${message}\n`);
|
|
143
|
+
if (tryThis) {
|
|
144
|
+
writeErr(`[next] ${tryThis}\n`);
|
|
145
|
+
}
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
writeErr(` ${paint(ANSI.red, "✗")} ${message}\n`);
|
|
149
|
+
if (tryThis) {
|
|
150
|
+
writeErr(` ${paint(ANSI.dim, tryThis)}\n`);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
const blank = () => {
|
|
154
|
+
if (json || plain)
|
|
155
|
+
return;
|
|
156
|
+
writeOut("\n");
|
|
157
|
+
};
|
|
158
|
+
const box = (title, lines) => {
|
|
159
|
+
if (json)
|
|
160
|
+
return;
|
|
161
|
+
const cleanLines = lines.map((line) => redactSecrets(line));
|
|
162
|
+
const cleanTitle = redactSecrets(title);
|
|
163
|
+
if (plain) {
|
|
164
|
+
writeOut(`[box] ${cleanTitle}\n`);
|
|
165
|
+
for (const line of cleanLines) {
|
|
166
|
+
writeOut(` ${line}\n`);
|
|
167
|
+
}
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
let contentWidth = stripAnsi(cleanTitle).length + 2;
|
|
171
|
+
for (const line of cleanLines) {
|
|
172
|
+
const length = stripAnsi(line).length;
|
|
173
|
+
if (length > contentWidth) {
|
|
174
|
+
contentWidth = length;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const innerWidth = Math.min(Math.max(contentWidth + 2, 36), (stdout.columns ?? 80) - 6);
|
|
178
|
+
const titleSegment = ` ${cleanTitle} `;
|
|
179
|
+
const topFill = Math.max(1, innerWidth - stripAnsi(titleSegment).length);
|
|
180
|
+
writeOut(`\n ${paint(ANSI.cyan, "╭─")}${paint(ANSI.bold + ANSI.cyan, titleSegment)}${paint(ANSI.cyan, `${"─".repeat(topFill)}╮`)}\n`);
|
|
181
|
+
for (const line of cleanLines) {
|
|
182
|
+
const visible = stripAnsi(line).length;
|
|
183
|
+
const pad = Math.max(0, innerWidth - visible);
|
|
184
|
+
writeOut(` ${paint(ANSI.cyan, "│")} ${line}${" ".repeat(pad)} ${paint(ANSI.cyan, "│")}\n`);
|
|
185
|
+
}
|
|
186
|
+
writeOut(` ${paint(ANSI.cyan, `╰${"─".repeat(innerWidth + 2)}╯`)}\n\n`);
|
|
187
|
+
};
|
|
188
|
+
const spinner = (initialLabel) => {
|
|
189
|
+
let label = initialLabel;
|
|
190
|
+
const startedAt = now();
|
|
191
|
+
let frameIndex = 0;
|
|
192
|
+
let active = true;
|
|
193
|
+
let timer = null;
|
|
194
|
+
if (plain || json) {
|
|
195
|
+
if (!json) {
|
|
196
|
+
writeOut(`[step] ${label}\n`);
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
update(nextLabel) {
|
|
200
|
+
label = nextLabel;
|
|
201
|
+
if (!json) {
|
|
202
|
+
writeOut(`[step] ${label}\n`);
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
succeed(doneLabel) {
|
|
206
|
+
if (!active)
|
|
207
|
+
return;
|
|
208
|
+
active = false;
|
|
209
|
+
ok(doneLabel ?? label);
|
|
210
|
+
},
|
|
211
|
+
warn(warnLabel) {
|
|
212
|
+
if (!active)
|
|
213
|
+
return;
|
|
214
|
+
active = false;
|
|
215
|
+
warn(warnLabel ?? label);
|
|
216
|
+
},
|
|
217
|
+
fail(failLabel) {
|
|
218
|
+
if (!active)
|
|
219
|
+
return;
|
|
220
|
+
active = false;
|
|
221
|
+
error(failLabel ?? label);
|
|
222
|
+
},
|
|
223
|
+
stop() {
|
|
224
|
+
active = false;
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
const renderFrame = () => {
|
|
229
|
+
if (!active)
|
|
230
|
+
return;
|
|
231
|
+
const frame = BRAILLE_FRAMES[frameIndex % BRAILLE_FRAMES.length];
|
|
232
|
+
frameIndex += 1;
|
|
233
|
+
const elapsed = formatElapsed(now() - startedAt);
|
|
234
|
+
writeOut(`${ANSI.clearLine} ${paint(ANSI.cyan, frame)} ${label} ${paint(ANSI.dim, `(${elapsed})`)}`);
|
|
235
|
+
};
|
|
236
|
+
const cleanup = () => {
|
|
237
|
+
if (timer) {
|
|
238
|
+
clearInterval(timer);
|
|
239
|
+
timer = null;
|
|
240
|
+
}
|
|
241
|
+
if (active) {
|
|
242
|
+
active = false;
|
|
243
|
+
writeOut(`${ANSI.clearLine}${ANSI.showCursor}`);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
writeOut(ANSI.hideCursor);
|
|
247
|
+
renderFrame();
|
|
248
|
+
timer = setInterval(renderFrame, 80);
|
|
249
|
+
timer.unref?.();
|
|
250
|
+
return {
|
|
251
|
+
update(nextLabel) {
|
|
252
|
+
label = nextLabel;
|
|
253
|
+
renderFrame();
|
|
254
|
+
},
|
|
255
|
+
succeed(doneLabel) {
|
|
256
|
+
cleanup();
|
|
257
|
+
ok(doneLabel ?? label);
|
|
258
|
+
},
|
|
259
|
+
warn(warnLabel) {
|
|
260
|
+
cleanup();
|
|
261
|
+
warn(warnLabel ?? label);
|
|
262
|
+
},
|
|
263
|
+
fail(failLabel) {
|
|
264
|
+
cleanup();
|
|
265
|
+
error(failLabel ?? label);
|
|
266
|
+
},
|
|
267
|
+
stop() {
|
|
268
|
+
cleanup();
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
};
|
|
272
|
+
const select = async (prompt, choices, defaultIndex = 0) => {
|
|
273
|
+
if (choices.length === 0) {
|
|
274
|
+
throw new Error("select() requires at least one option.");
|
|
275
|
+
}
|
|
276
|
+
const safeDefault = Math.min(Math.max(defaultIndex, 0), choices.length - 1);
|
|
277
|
+
if (!interactive) {
|
|
278
|
+
return choices[safeDefault].value;
|
|
279
|
+
}
|
|
280
|
+
let selectedIndex = safeDefault;
|
|
281
|
+
let renderedLines = 0;
|
|
282
|
+
const render = () => {
|
|
283
|
+
if (renderedLines > 0) {
|
|
284
|
+
writeOut(`\x1b[${renderedLines}A`);
|
|
285
|
+
}
|
|
286
|
+
const lines = [
|
|
287
|
+
` ${paint(ANSI.bold, prompt)} ${paint(ANSI.dim, "(arrows or 1-9, Enter to confirm)")}`,
|
|
288
|
+
];
|
|
289
|
+
choices.forEach((choice, index) => {
|
|
290
|
+
const active = index === selectedIndex;
|
|
291
|
+
const marker = active ? paint(ANSI.cyan, "❯") : " ";
|
|
292
|
+
const num = paint(ANSI.dim, `${index + 1}.`);
|
|
293
|
+
const labelText = active ? paint(ANSI.bold + ANSI.cyan, choice.label) : choice.label;
|
|
294
|
+
const hintText = choice.hint ? ` ${paint(ANSI.dim, choice.hint)}` : "";
|
|
295
|
+
lines.push(` ${marker} ${num} ${labelText}${hintText}`);
|
|
296
|
+
});
|
|
297
|
+
for (const line of lines) {
|
|
298
|
+
writeOut(`${ANSI.clearLine}${line}\n`);
|
|
299
|
+
}
|
|
300
|
+
renderedLines = lines.length;
|
|
301
|
+
};
|
|
302
|
+
return await new Promise((resolve, reject) => {
|
|
303
|
+
const wasRaw = Boolean(stdin.isRaw);
|
|
304
|
+
const restoreTerminal = () => {
|
|
305
|
+
stdin.off("keypress", onKeypress);
|
|
306
|
+
process.off("SIGINT", onSigint);
|
|
307
|
+
process.off("exit", restoreCursor);
|
|
308
|
+
if (stdin.setRawMode) {
|
|
309
|
+
try {
|
|
310
|
+
stdin.setRawMode(wasRaw);
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
// Ignore terminal restoration failures when stream is already closed.
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
stdin.pause();
|
|
317
|
+
writeOut(ANSI.showCursor);
|
|
318
|
+
};
|
|
319
|
+
const restoreCursor = () => {
|
|
320
|
+
stdout.write(ANSI.showCursor);
|
|
321
|
+
};
|
|
322
|
+
const onSigint = () => {
|
|
323
|
+
restoreTerminal();
|
|
324
|
+
reject(new Error("Cancelled by user."));
|
|
325
|
+
};
|
|
326
|
+
const onKeypress = (str, key) => {
|
|
327
|
+
if (key?.ctrl && key.name === "c") {
|
|
328
|
+
onSigint();
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
if (key?.name === "up" || key?.name === "k") {
|
|
332
|
+
selectedIndex = (selectedIndex - 1 + choices.length) % choices.length;
|
|
333
|
+
render();
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (key?.name === "down" || key?.name === "j") {
|
|
337
|
+
selectedIndex = (selectedIndex + 1) % choices.length;
|
|
338
|
+
render();
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (str && /^[1-9]$/.test(str)) {
|
|
342
|
+
const picked = Number(str) - 1;
|
|
343
|
+
if (picked >= 0 && picked < choices.length) {
|
|
344
|
+
selectedIndex = picked;
|
|
345
|
+
render();
|
|
346
|
+
restoreTerminal();
|
|
347
|
+
resolve(choices[selectedIndex].value);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (key?.name === "return" || key?.name === "enter") {
|
|
352
|
+
restoreTerminal();
|
|
353
|
+
resolve(choices[selectedIndex].value);
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
emitKeypressEvents(stdin);
|
|
357
|
+
if (stdin.setRawMode) {
|
|
358
|
+
stdin.setRawMode(true);
|
|
359
|
+
}
|
|
360
|
+
stdin.resume();
|
|
361
|
+
writeOut(ANSI.hideCursor);
|
|
362
|
+
process.on("SIGINT", onSigint);
|
|
363
|
+
process.on("exit", restoreCursor);
|
|
364
|
+
stdin.on("keypress", onKeypress);
|
|
365
|
+
render();
|
|
366
|
+
});
|
|
367
|
+
};
|
|
368
|
+
const confirm = async (prompt, defaultValue = true) => {
|
|
369
|
+
if (!interactive) {
|
|
370
|
+
return defaultValue;
|
|
371
|
+
}
|
|
372
|
+
const answer = await select(prompt, [
|
|
373
|
+
{ value: "yes", label: "Yes" },
|
|
374
|
+
{ value: "no", label: "No" },
|
|
375
|
+
], defaultValue ? 0 : 1);
|
|
376
|
+
return answer === "yes";
|
|
377
|
+
};
|
|
378
|
+
const emitJson = (payload) => {
|
|
379
|
+
const clean = redactJsonValue(payload);
|
|
380
|
+
stdout.write(`${JSON.stringify(clean, null, 2)}\n`);
|
|
381
|
+
};
|
|
382
|
+
return {
|
|
383
|
+
interactive,
|
|
384
|
+
plain,
|
|
385
|
+
json,
|
|
386
|
+
header,
|
|
387
|
+
step,
|
|
388
|
+
info,
|
|
389
|
+
ok,
|
|
390
|
+
warn,
|
|
391
|
+
error,
|
|
392
|
+
blank,
|
|
393
|
+
box,
|
|
394
|
+
spinner,
|
|
395
|
+
select,
|
|
396
|
+
confirm,
|
|
397
|
+
emitJson,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { createHmac, randomUUID } from "node:crypto";
|
|
2
|
+
export const WEBHOOK_EVENT_TYPES = [
|
|
3
|
+
"orbit.draft_ready",
|
|
4
|
+
"orbit.price_ready",
|
|
5
|
+
"orbit.safeguard_ready",
|
|
6
|
+
"orbit.finished",
|
|
7
|
+
"orbit.failed",
|
|
8
|
+
"safeguard.review_finished",
|
|
9
|
+
"pixeltrace.capture_ready",
|
|
10
|
+
"pixeltrace.capture_completed",
|
|
11
|
+
"ask.document_changed",
|
|
12
|
+
"message_check.flagged",
|
|
13
|
+
"dispute_pack.ready",
|
|
14
|
+
"dispute_pack.released",
|
|
15
|
+
"dispute_pack.email_problem",
|
|
16
|
+
];
|
|
17
|
+
const EVENT_SET = new Set(WEBHOOK_EVENT_TYPES);
|
|
18
|
+
export function isWebhookEventType(value) {
|
|
19
|
+
return EVENT_SET.has(value);
|
|
20
|
+
}
|
|
21
|
+
const DEFAULT_SAMPLE_MARKETPLACE_ID = "00000000-0000-4000-8000-000000000001";
|
|
22
|
+
function isStringPayload(value) {
|
|
23
|
+
return typeof value === "string";
|
|
24
|
+
}
|
|
25
|
+
function buildEventData(eventType) {
|
|
26
|
+
const resourceId = randomUUID();
|
|
27
|
+
const shortId = resourceId.slice(0, 8);
|
|
28
|
+
switch (eventType) {
|
|
29
|
+
case "orbit.draft_ready":
|
|
30
|
+
return {
|
|
31
|
+
id: resourceId,
|
|
32
|
+
resource: "orbit",
|
|
33
|
+
status: "listing",
|
|
34
|
+
};
|
|
35
|
+
case "orbit.price_ready":
|
|
36
|
+
return {
|
|
37
|
+
id: resourceId,
|
|
38
|
+
resource: "orbit",
|
|
39
|
+
status: "listing",
|
|
40
|
+
};
|
|
41
|
+
case "orbit.safeguard_ready":
|
|
42
|
+
return {
|
|
43
|
+
id: resourceId,
|
|
44
|
+
resource: "orbit",
|
|
45
|
+
status: "listing",
|
|
46
|
+
};
|
|
47
|
+
case "orbit.finished":
|
|
48
|
+
return {
|
|
49
|
+
id: resourceId,
|
|
50
|
+
resource: "orbit",
|
|
51
|
+
status: "listing",
|
|
52
|
+
};
|
|
53
|
+
case "orbit.failed":
|
|
54
|
+
return {
|
|
55
|
+
id: resourceId,
|
|
56
|
+
resource: "orbit",
|
|
57
|
+
status: "failed",
|
|
58
|
+
};
|
|
59
|
+
case "safeguard.review_finished":
|
|
60
|
+
return {
|
|
61
|
+
id: resourceId,
|
|
62
|
+
resource: "safeguard",
|
|
63
|
+
status: "done",
|
|
64
|
+
};
|
|
65
|
+
case "pixeltrace.capture_ready":
|
|
66
|
+
return {
|
|
67
|
+
id: resourceId,
|
|
68
|
+
resource: "pixeltrace",
|
|
69
|
+
status: "ready",
|
|
70
|
+
};
|
|
71
|
+
case "pixeltrace.capture_completed":
|
|
72
|
+
return {
|
|
73
|
+
id: resourceId,
|
|
74
|
+
resource: "pixeltrace",
|
|
75
|
+
status: "completed",
|
|
76
|
+
};
|
|
77
|
+
case "ask.document_changed":
|
|
78
|
+
return {
|
|
79
|
+
id: resourceId,
|
|
80
|
+
resource: "document",
|
|
81
|
+
status: "ready",
|
|
82
|
+
};
|
|
83
|
+
case "message_check.flagged":
|
|
84
|
+
return {
|
|
85
|
+
id: resourceId,
|
|
86
|
+
resource: "message_check_flag",
|
|
87
|
+
status: "block",
|
|
88
|
+
};
|
|
89
|
+
case "dispute_pack.ready":
|
|
90
|
+
return {
|
|
91
|
+
id: resourceId,
|
|
92
|
+
resource: "dispute_pack",
|
|
93
|
+
status: "ready",
|
|
94
|
+
error: null,
|
|
95
|
+
disputeId: `disp_${shortId}`,
|
|
96
|
+
listingCode: `lst_${shortId}`,
|
|
97
|
+
version: 1,
|
|
98
|
+
reportUrl: `https://business.pcific.ai/api/v1/dispute-pack/disputes/${resourceId}?version=1`,
|
|
99
|
+
pdfUrl: `https://business.pcific.ai/api/v1/dispute-pack/disputes/${resourceId}/pdf?version=1`,
|
|
100
|
+
suggestedEmail: null,
|
|
101
|
+
};
|
|
102
|
+
case "dispute_pack.released": {
|
|
103
|
+
const releaseId = randomUUID();
|
|
104
|
+
const releasePdfUrl = `https://business.pcific.ai/api/v1/dispute-pack/releases/${releaseId}/pdf`;
|
|
105
|
+
return {
|
|
106
|
+
id: resourceId,
|
|
107
|
+
resource: "dispute_pack",
|
|
108
|
+
status: "released",
|
|
109
|
+
releaseId,
|
|
110
|
+
disputeId: `disp_${shortId}`,
|
|
111
|
+
listingCode: `lst_${shortId}`,
|
|
112
|
+
version: 1,
|
|
113
|
+
sections: ["summary", "timeline", "evidence"],
|
|
114
|
+
delivery: "pdf",
|
|
115
|
+
link: null,
|
|
116
|
+
linkShown: false,
|
|
117
|
+
expiresAt: null,
|
|
118
|
+
pdfUrl: releasePdfUrl,
|
|
119
|
+
releasedBy: { type: "api_key", name: "Test key" },
|
|
120
|
+
revokedAt: null,
|
|
121
|
+
suggestedEmail: {
|
|
122
|
+
to: "seller@example.com",
|
|
123
|
+
subject: `Evidence for order ord_${shortId}`,
|
|
124
|
+
body: "Please find the dispute evidence attached as a PDF.",
|
|
125
|
+
sellerLink: null,
|
|
126
|
+
pdfUrl: releasePdfUrl,
|
|
127
|
+
pdfFileName: `dispute-evidence-order-ord-${shortId}.pdf`,
|
|
128
|
+
note: "Download the PDF from pdfUrl and attach it, named as pdfFileName, before you send this email.",
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
case "dispute_pack.email_problem":
|
|
133
|
+
return {
|
|
134
|
+
id: resourceId,
|
|
135
|
+
resource: "dispute_pack",
|
|
136
|
+
disputeId: `disp_${shortId}`,
|
|
137
|
+
listingCode: `lst_${shortId}`,
|
|
138
|
+
emailId: randomUUID(),
|
|
139
|
+
email: "sellerRelease",
|
|
140
|
+
to: "seller@example.com",
|
|
141
|
+
status: "bounced",
|
|
142
|
+
problem: "bounced",
|
|
143
|
+
message: "The seller's mail server refused this email. Check the address with the seller, or send the suggested email another way.",
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export function buildSampleWebhookEvent(eventType, options = {}) {
|
|
148
|
+
return {
|
|
149
|
+
id: options.eventId ?? randomUUID(),
|
|
150
|
+
type: eventType,
|
|
151
|
+
createdAt: options.createdAt ?? new Date().toISOString(),
|
|
152
|
+
marketplaceId: options.marketplaceId ?? DEFAULT_SAMPLE_MARKETPLACE_ID,
|
|
153
|
+
test: options.test ?? true,
|
|
154
|
+
data: buildEventData(eventType),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Produces the exact `PCIFIC-Signature` header value (`t=<unix>,v1=<hex>`):
|
|
159
|
+
* HMAC-SHA256 over `${timestamp}.${rawBody}` using `secret` as UTF-8 bytes.
|
|
160
|
+
*/
|
|
161
|
+
export function signWebhookPayload(rawBody, secret, timestamp = Math.floor(Date.now() / 1000)) {
|
|
162
|
+
const hmac = createHmac("sha256", secret);
|
|
163
|
+
hmac.update(`${timestamp}.`);
|
|
164
|
+
if (isStringPayload(rawBody)) {
|
|
165
|
+
hmac.update(rawBody, "utf8");
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
hmac.update(rawBody);
|
|
169
|
+
}
|
|
170
|
+
return `t=${timestamp},v1=${hmac.digest("hex")}`;
|
|
171
|
+
}
|