@x12i/helpers 1.0.1 → 1.1.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.
@@ -0,0 +1,398 @@
1
+ /**
2
+ * ╔══════════════════════════════════════════════════════════════╗
3
+ * ║ API CONTRACTS ║
4
+ * ╠══════════════════════════════════════════════════════════════╣
5
+ * ║ Define API protocols, request/response shapes, validation, ║
6
+ * ║ serialization, and authentication schemes. ║
7
+ * ╚══════════════════════════════════════════════════════════════╝
8
+ *
9
+ * A "contract" fully describes one side of an API call:
10
+ * - protocol (rest | graphql | jsonrpc | soap | custom)
11
+ * - endpoint (URL template, method, content-type)
12
+ * - auth (bearer | apikey | basic | header | query | oauth2 | custom)
13
+ * - request schema (params, headers, body)
14
+ * - response schema (body, headers, status mapping)
15
+ * - serializer / deserializer per protocol
16
+ */
17
+
18
+ // ═══════════════════════════════════════════════════════════════
19
+ // VALIDATION (lightweight JSON-schema-ish)
20
+ // ═══════════════════════════════════════════════════════════════
21
+
22
+ const TYPES = {
23
+ string: (v) => typeof v === "string",
24
+ number: (v) => typeof v === "number" && !Number.isNaN(v),
25
+ boolean: (v) => typeof v === "boolean",
26
+ object: (v) => v !== null && typeof v === "object" && !Array.isArray(v),
27
+ array: (v) => Array.isArray(v),
28
+ any: () => true,
29
+ };
30
+
31
+ function validateSchema(data, schema, path = "") {
32
+ const errors = [];
33
+ if (!schema || typeof schema !== "object") return errors;
34
+ if (!data || typeof data !== "object") return errors;
35
+
36
+ for (const [field, rules] of Object.entries(schema)) {
37
+ if (!rules || typeof rules !== "object") continue;
38
+ const fullPath = path ? `${path}.${field}` : field;
39
+ const value = data[field];
40
+
41
+ if (rules.required && (value === undefined || value === null)) {
42
+ errors.push({ path: fullPath, message: "required" });
43
+ continue;
44
+ }
45
+ if (value === undefined || value === null) continue;
46
+
47
+ if (rules.type && TYPES[rules.type] && !TYPES[rules.type](value)) {
48
+ errors.push({
49
+ path: fullPath,
50
+ message: `expected ${rules.type}`,
51
+ got: typeof value,
52
+ });
53
+ }
54
+ if (rules.enum && !rules.enum.includes(value)) {
55
+ errors.push({
56
+ path: fullPath,
57
+ message: `must be one of [${rules.enum}]`,
58
+ got: value,
59
+ });
60
+ }
61
+ if (rules.pattern && typeof value === "string" && !new RegExp(rules.pattern).test(value)) {
62
+ errors.push({ path: fullPath, message: `must match ${rules.pattern}` });
63
+ }
64
+ if (rules.min !== undefined && value < rules.min) {
65
+ errors.push({ path: fullPath, message: `min ${rules.min}`, got: value });
66
+ }
67
+ if (rules.max !== undefined && value > rules.max) {
68
+ errors.push({ path: fullPath, message: `max ${rules.max}`, got: value });
69
+ }
70
+ if (rules.properties && typeof value === "object") {
71
+ errors.push(...validateSchema(value, rules.properties, fullPath));
72
+ }
73
+ if (rules.items && Array.isArray(value)) {
74
+ value.forEach((el, i) => {
75
+ if (rules.items.type && TYPES[rules.items.type] && !TYPES[rules.items.type](el)) {
76
+ errors.push({ path: `${fullPath}[${i}]`, message: `expected ${rules.items.type}` });
77
+ }
78
+ if (rules.items.properties && typeof el === "object") {
79
+ errors.push(...validateSchema(el, rules.items.properties, `${fullPath}[${i}]`));
80
+ }
81
+ });
82
+ }
83
+ if (rules.validate && !rules.validate(value, data)) {
84
+ errors.push({ path: fullPath, message: rules.validateMsg || "custom validation failed" });
85
+ }
86
+ }
87
+ return errors;
88
+ }
89
+
90
+ // ═══════════════════════════════════════════════════════════════
91
+ // AUTH SCHEMES
92
+ // ═══════════════════════════════════════════════════════════════
93
+
94
+ const AUTH_HANDLERS = {
95
+ bearer: (creds, req) => {
96
+ req.headers = req.headers || {};
97
+ req.headers["Authorization"] = `Bearer ${creds.token}`;
98
+ },
99
+ basic: (creds, req) => {
100
+ req.headers = req.headers || {};
101
+ const encoded =
102
+ typeof btoa === "function"
103
+ ? btoa(`${creds.username}:${creds.password}`)
104
+ : Buffer.from(`${creds.username}:${creds.password}`).toString("base64");
105
+ req.headers["Authorization"] = `Basic ${encoded}`;
106
+ },
107
+ apikey: (creds, req) => {
108
+ if (creds.in === "query") {
109
+ req.query = req.query || {};
110
+ req.query[creds.name || "api_key"] = creds.key;
111
+ } else {
112
+ req.headers = req.headers || {};
113
+ req.headers[creds.name || "X-API-Key"] = creds.key;
114
+ }
115
+ },
116
+ header: (creds, req) => {
117
+ req.headers = req.headers || {};
118
+ req.headers[creds.name] = creds.value;
119
+ },
120
+ query: (creds, req) => {
121
+ req.query = req.query || {};
122
+ req.query[creds.name] = creds.value;
123
+ },
124
+ oauth2: (creds, req) => {
125
+ req.headers = req.headers || {};
126
+ req.headers["Authorization"] = `Bearer ${creds.accessToken}`;
127
+ },
128
+ custom: (creds, req) => {
129
+ if (creds.apply) creds.apply(req);
130
+ },
131
+ };
132
+
133
+ function applyAuth(authConfig, credentials, request) {
134
+ if (!authConfig || !credentials) return;
135
+ const handler = AUTH_HANDLERS[authConfig.type];
136
+ if (handler) handler(credentials, request);
137
+ }
138
+
139
+ // ═══════════════════════════════════════════════════════════════
140
+ // PROTOCOL SERIALIZERS
141
+ // ═══════════════════════════════════════════════════════════════
142
+
143
+ function escapeXml(val) {
144
+ if (val == null) return "";
145
+ return String(val)
146
+ .replace(/&/g, "&amp;")
147
+ .replace(/</g, "&lt;")
148
+ .replace(/>/g, "&gt;")
149
+ .replace(/"/g, "&quot;");
150
+ }
151
+
152
+ const PROTOCOLS = {
153
+ // ── REST ───────────────────────────────────
154
+ rest: {
155
+ name: "REST",
156
+ buildRequest(contract, params) {
157
+ let url = contract.endpoint || "";
158
+ const { pathParams = {}, queryParams = {}, body = null, headers = {} } = params;
159
+
160
+ url = url.replace(/[:{}](\w+)}?/g, (_, key) => {
161
+ const val = pathParams[key];
162
+ if (val === undefined) throw new Error(`Missing path param: ${key}`);
163
+ return encodeURIComponent(val);
164
+ });
165
+
166
+ return {
167
+ url,
168
+ method: (contract.method || "GET").toUpperCase(),
169
+ headers: {
170
+ "Content-Type": contract.contentType || "application/json",
171
+ ...contract.defaultHeaders,
172
+ ...headers,
173
+ },
174
+ query: { ...contract.defaultQuery, ...queryParams },
175
+ body: body != null ? body : undefined,
176
+ };
177
+ },
178
+ parseResponse(_c, raw) {
179
+ return {
180
+ status: raw.status,
181
+ headers: raw.headers || {},
182
+ data: raw.body,
183
+ error: raw.status >= 400 ? raw.body?.error || raw.body?.message || raw.statusText : null,
184
+ };
185
+ },
186
+ },
187
+
188
+ // ── GRAPHQL ────────────────────────────────
189
+ graphql: {
190
+ name: "GraphQL",
191
+ buildRequest(contract, params) {
192
+ const { variables = {}, headers = {}, operationName } = params;
193
+ const query = params.query || contract.query || contract.mutation;
194
+ if (!query) throw new Error("GraphQL contract requires query or mutation");
195
+
196
+ return {
197
+ url: contract.endpoint,
198
+ method: "POST",
199
+ headers: { "Content-Type": "application/json", ...contract.defaultHeaders, ...headers },
200
+ query: {},
201
+ body: { query, variables, ...(operationName ? { operationName } : {}) },
202
+ };
203
+ },
204
+ parseResponse(_c, raw) {
205
+ const body = raw.body || {};
206
+ return {
207
+ status: raw.status,
208
+ headers: raw.headers || {},
209
+ data: body.data || null,
210
+ error: body.errors ? body.errors.map((e) => e.message).join("; ") : null,
211
+ };
212
+ },
213
+ },
214
+
215
+ // ── JSON-RPC 2.0 ──────────────────────────
216
+ jsonrpc: {
217
+ name: "JSON-RPC 2.0",
218
+ buildRequest(contract, params) {
219
+ const { rpcParams = {}, headers = {}, id = 1 } = params;
220
+ return {
221
+ url: contract.endpoint,
222
+ method: "POST",
223
+ headers: { "Content-Type": "application/json", ...contract.defaultHeaders, ...headers },
224
+ query: {},
225
+ body: {
226
+ jsonrpc: "2.0",
227
+ id,
228
+ method: contract.rpcMethod,
229
+ params: contract.paramsAsArray ? Object.values(rpcParams) : rpcParams,
230
+ },
231
+ };
232
+ },
233
+ parseResponse(_c, raw) {
234
+ const body = raw.body || {};
235
+ return {
236
+ status: raw.status,
237
+ headers: raw.headers || {},
238
+ data: body.result ?? null,
239
+ error: body.error ? `${body.error.code}: ${body.error.message}` : null,
240
+ };
241
+ },
242
+ },
243
+
244
+ // ── SOAP ───────────────────────────────────
245
+ soap: {
246
+ name: "SOAP",
247
+ buildRequest(contract, params) {
248
+ const { soapBody = {}, headers = {}, soapHeaders = {} } = params;
249
+ const ns = contract.namespace || "http://tempuri.org/";
250
+ const action = contract.soapAction || contract.operation;
251
+
252
+ const bodyXml = Object.entries(soapBody)
253
+ .map(([k, v]) => ` <ns:${k}>${escapeXml(v)}</ns:${k}>`)
254
+ .join("\n");
255
+ const headerXml = Object.entries(soapHeaders)
256
+ .map(([k, v]) => ` <ns:${k}>${escapeXml(v)}</ns:${k}>`)
257
+ .join("\n");
258
+
259
+ const envelope = `<?xml version="1.0" encoding="utf-8"?>
260
+ <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="${ns}">
261
+ <soap:Header>\n${headerXml}\n </soap:Header>
262
+ <soap:Body>
263
+ <ns:${contract.operation}>\n${bodyXml}\n </ns:${contract.operation}>
264
+ </soap:Body>
265
+ </soap:Envelope>`;
266
+
267
+ return {
268
+ url: contract.endpoint,
269
+ method: "POST",
270
+ headers: {
271
+ "Content-Type": "text/xml; charset=utf-8",
272
+ SOAPAction: action,
273
+ ...contract.defaultHeaders,
274
+ ...headers,
275
+ },
276
+ query: {},
277
+ body: envelope,
278
+ };
279
+ },
280
+ parseResponse(contract, raw) {
281
+ const data = contract.responseExtractor ? contract.responseExtractor(raw.body) : raw.body;
282
+ return {
283
+ status: raw.status,
284
+ headers: raw.headers || {},
285
+ data,
286
+ error: raw.status >= 400 ? raw.body : null,
287
+ };
288
+ },
289
+ },
290
+
291
+ // ── CUSTOM ─────────────────────────────────
292
+ custom: {
293
+ name: "Custom",
294
+ buildRequest(contract, params) {
295
+ if (!contract.buildRequest) throw new Error("Custom protocol needs buildRequest()");
296
+ return contract.buildRequest(params);
297
+ },
298
+ parseResponse(contract, raw) {
299
+ if (!contract.parseResponse) return { status: raw.status, data: raw.body, error: null };
300
+ return contract.parseResponse(raw);
301
+ },
302
+ },
303
+ };
304
+
305
+ // ═══════════════════════════════════════════════════════════════
306
+ // CONTRACT BUILDER
307
+ // ═══════════════════════════════════════════════════════════════
308
+
309
+ /**
310
+ * @param {object} config
311
+ * @param {string} config.name – "getUser", "createOrder"
312
+ * @param {string} config.protocol – "rest"|"graphql"|"jsonrpc"|"soap"|"custom"
313
+ * @param {string} config.endpoint – URL or URL template
314
+ * @param {string} [config.method] – HTTP method (REST)
315
+ * @param {string} [config.query] – GQL query
316
+ * @param {string} [config.mutation] – GQL mutation
317
+ * @param {string} [config.rpcMethod] – JSON-RPC method name
318
+ * @param {string} [config.operation] – SOAP operation
319
+ * @param {string} [config.namespace] – SOAP namespace
320
+ * @param {string} [config.soapAction] – SOAP action header
321
+ * @param {object} [config.auth] – { type, ... }
322
+ * @param {object} [config.requestSchema] – validation for params
323
+ * @param {object} [config.responseSchema] – validation for response data
324
+ * @param {object} [config.defaultHeaders]
325
+ * @param {object} [config.defaultQuery]
326
+ * @param {string} [config.contentType]
327
+ * @param {function} [config.buildRequest] – custom protocol
328
+ * @param {function} [config.parseResponse] – custom protocol
329
+ * @param {function} [config.responseExtractor] – SOAP
330
+ * @param {boolean} [config.paramsAsArray] – JSON-RPC
331
+ */
332
+ function defineContract(config) {
333
+ const protocol = PROTOCOLS[config.protocol];
334
+ if (!protocol) {
335
+ throw new Error(`Unknown protocol "${config.protocol}". Use: ${Object.keys(PROTOCOLS).join(", ")}`);
336
+ }
337
+
338
+ return {
339
+ ...config,
340
+ _protocol: protocol,
341
+
342
+ validateRequest(params) {
343
+ if (!config.requestSchema) return { valid: true, errors: [] };
344
+ const errors = validateSchema(params, config.requestSchema);
345
+ return { valid: errors.length === 0, errors };
346
+ },
347
+
348
+ validateResponse(data) {
349
+ if (!config.responseSchema) return { valid: true, errors: [] };
350
+ const safe = data && typeof data === "object" ? data : {};
351
+ const errors = validateSchema(safe, config.responseSchema);
352
+ return { valid: errors.length === 0, errors };
353
+ },
354
+
355
+ buildRequest(params, credentials) {
356
+ const req = protocol.buildRequest(config, params);
357
+ if (config.auth && credentials) applyAuth(config.auth, credentials, req);
358
+ return req;
359
+ },
360
+
361
+ parseResponse(raw) {
362
+ return protocol.parseResponse(config, raw);
363
+ },
364
+ };
365
+ }
366
+
367
+ // ═══════════════════════════════════════════════════════════════
368
+ // CONTRACT REGISTRY
369
+ // ═══════════════════════════════════════════════════════════════
370
+
371
+ function createRegistry() {
372
+ const contracts = new Map();
373
+ return {
374
+ register(config) {
375
+ const c = config._protocol ? config : defineContract(config);
376
+ contracts.set(c.name, c);
377
+ return c;
378
+ },
379
+ get(name) {
380
+ const c = contracts.get(name);
381
+ if (!c) throw new Error(`Contract "${name}" not registered`);
382
+ return c;
383
+ },
384
+ has: (name) => contracts.has(name),
385
+ list: () => [...contracts.keys()],
386
+ remove: (name) => contracts.delete(name),
387
+ };
388
+ }
389
+
390
+ module.exports = {
391
+ defineContract,
392
+ createRegistry,
393
+ validateSchema,
394
+ applyAuth,
395
+ PROTOCOLS,
396
+ AUTH_HANDLERS,
397
+ };
398
+