@hookflo/tern 4.3.0-beta.0 → 4.3.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +31 -0
  2. package/dist/adapters/cloudflare.d.ts +2 -2
  3. package/dist/adapters/cloudflare.js +6 -1
  4. package/dist/adapters/express.d.ts +2 -2
  5. package/dist/adapters/express.js +6 -1
  6. package/dist/adapters/hono.d.ts +2 -2
  7. package/dist/adapters/hono.js +6 -1
  8. package/dist/adapters/nextjs.d.ts +2 -2
  9. package/dist/adapters/nextjs.js +6 -1
  10. package/dist/adapters/shared.js +5 -2
  11. package/dist/index.d.ts +3 -4
  12. package/dist/index.js +29 -19
  13. package/dist/platforms/algorithms.js +65 -0
  14. package/dist/types.d.ts +6 -65
  15. package/dist/types.js +4 -0
  16. package/dist/verifiers/algorithms.d.ts +8 -0
  17. package/dist/verifiers/algorithms.js +145 -17
  18. package/package.json +1 -1
  19. package/dist/normalization/index.d.ts +0 -20
  20. package/dist/normalization/index.js +0 -78
  21. package/dist/normalization/providers/payment/paypal.d.ts +0 -2
  22. package/dist/normalization/providers/payment/paypal.js +0 -12
  23. package/dist/normalization/providers/payment/razorpay.d.ts +0 -2
  24. package/dist/normalization/providers/payment/razorpay.js +0 -13
  25. package/dist/normalization/providers/payment/stripe.d.ts +0 -2
  26. package/dist/normalization/providers/payment/stripe.js +0 -13
  27. package/dist/normalization/providers/registry.d.ts +0 -5
  28. package/dist/normalization/providers/registry.js +0 -21
  29. package/dist/normalization/simple.d.ts +0 -4
  30. package/dist/normalization/simple.js +0 -126
  31. package/dist/normalization/storage/interface.d.ts +0 -13
  32. package/dist/normalization/storage/interface.js +0 -2
  33. package/dist/normalization/storage/memory.d.ts +0 -12
  34. package/dist/normalization/storage/memory.js +0 -39
  35. package/dist/normalization/templates/base/auth.d.ts +0 -2
  36. package/dist/normalization/templates/base/auth.js +0 -22
  37. package/dist/normalization/templates/base/ecommerce.d.ts +0 -2
  38. package/dist/normalization/templates/base/ecommerce.js +0 -25
  39. package/dist/normalization/templates/base/payment.d.ts +0 -2
  40. package/dist/normalization/templates/base/payment.js +0 -25
  41. package/dist/normalization/templates/registry.d.ts +0 -6
  42. package/dist/normalization/templates/registry.js +0 -22
  43. package/dist/normalization/transformer/engine.d.ts +0 -11
  44. package/dist/normalization/transformer/engine.js +0 -86
  45. package/dist/normalization/transformer/validator.d.ts +0 -12
  46. package/dist/normalization/transformer/validator.js +0 -56
  47. package/dist/normalization/types.d.ts +0 -79
  48. package/dist/normalization/types.js +0 -2
@@ -1,39 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InMemoryStorageAdapter = void 0;
4
- const registry_1 = require("../templates/registry");
5
- class InMemoryStorageAdapter {
6
- constructor() {
7
- this.schemas = new Map();
8
- }
9
- async saveSchema(schema) {
10
- this.schemas.set(schema.id, schema);
11
- }
12
- async getSchema(id) {
13
- return this.schemas.get(id) ?? null;
14
- }
15
- async updateSchema(id, updates) {
16
- const existing = this.schemas.get(id);
17
- if (!existing)
18
- return;
19
- const updated = {
20
- ...existing,
21
- ...updates,
22
- updatedAt: new Date(),
23
- };
24
- this.schemas.set(id, updated);
25
- }
26
- async deleteSchema(id) {
27
- this.schemas.delete(id);
28
- }
29
- async listSchemas(userId) {
30
- return Array.from(this.schemas.values()).filter((s) => s.userId === userId);
31
- }
32
- async getBaseTemplate(id) {
33
- return registry_1.templateRegistry.getById(id) ?? null;
34
- }
35
- async listBaseTemplates() {
36
- return registry_1.templateRegistry.listAll();
37
- }
38
- }
39
- exports.InMemoryStorageAdapter = InMemoryStorageAdapter;
@@ -1,2 +0,0 @@
1
- import { BaseTemplate } from '../../types';
2
- export declare const authBaseTemplate: BaseTemplate;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.authBaseTemplate = void 0;
4
- exports.authBaseTemplate = {
5
- id: 'auth_v1',
6
- category: 'auth',
7
- version: '1.0.0',
8
- fields: [
9
- {
10
- id: 'event_type', name: 'event_type', type: 'string', required: true,
11
- },
12
- {
13
- id: 'user_id', name: 'user_id', type: 'string', required: true,
14
- },
15
- {
16
- id: 'email', name: 'email', type: 'string', required: false,
17
- },
18
- {
19
- id: 'status', name: 'status', type: 'string', required: true,
20
- },
21
- ],
22
- };
@@ -1,2 +0,0 @@
1
- import { BaseTemplate } from '../../types';
2
- export declare const ecommerceBaseTemplate: BaseTemplate;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ecommerceBaseTemplate = void 0;
4
- exports.ecommerceBaseTemplate = {
5
- id: 'ecommerce_v1',
6
- category: 'ecommerce',
7
- version: '1.0.0',
8
- fields: [
9
- {
10
- id: 'event_type', name: 'event_type', type: 'string', required: true,
11
- },
12
- {
13
- id: 'order_id', name: 'order_id', type: 'string', required: true,
14
- },
15
- {
16
- id: 'total', name: 'total', type: 'number', required: true,
17
- },
18
- {
19
- id: 'currency', name: 'currency', type: 'string', required: true,
20
- },
21
- {
22
- id: 'customer_id', name: 'customer_id', type: 'string', required: false,
23
- },
24
- ],
25
- };
@@ -1,2 +0,0 @@
1
- import { BaseTemplate } from '../../types';
2
- export declare const paymentBaseTemplate: BaseTemplate;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.paymentBaseTemplate = void 0;
4
- exports.paymentBaseTemplate = {
5
- id: 'payment_v1',
6
- category: 'payment',
7
- version: '1.0.0',
8
- fields: [
9
- {
10
- id: 'event_type', name: 'event_type', type: 'string', required: true, description: 'Type of payment event',
11
- },
12
- {
13
- id: 'amount', name: 'amount', type: 'number', required: true, description: 'Amount in the smallest currency unit',
14
- },
15
- {
16
- id: 'currency', name: 'currency', type: 'string', required: true, description: 'Three-letter currency code',
17
- },
18
- {
19
- id: 'transaction_id', name: 'transaction_id', type: 'string', required: true, description: 'Unique transaction identifier',
20
- },
21
- {
22
- id: 'customer_id', name: 'customer_id', type: 'string', required: false, description: 'Customer identifier',
23
- },
24
- ],
25
- };
@@ -1,6 +0,0 @@
1
- import { BaseTemplate, TemplateCategory } from '../types';
2
- export declare const templateRegistry: {
3
- getById(id: string): BaseTemplate | undefined;
4
- listByCategory(category: TemplateCategory): BaseTemplate[];
5
- listAll(): BaseTemplate[];
6
- };
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.templateRegistry = void 0;
4
- const payment_1 = require("./base/payment");
5
- const auth_1 = require("./base/auth");
6
- const ecommerce_1 = require("./base/ecommerce");
7
- const templates = {
8
- [payment_1.paymentBaseTemplate.id]: payment_1.paymentBaseTemplate,
9
- [auth_1.authBaseTemplate.id]: auth_1.authBaseTemplate,
10
- [ecommerce_1.ecommerceBaseTemplate.id]: ecommerce_1.ecommerceBaseTemplate,
11
- };
12
- exports.templateRegistry = {
13
- getById(id) {
14
- return templates[id];
15
- },
16
- listByCategory(category) {
17
- return Object.values(templates).filter((t) => t.category === category);
18
- },
19
- listAll() {
20
- return Object.values(templates);
21
- },
22
- };
@@ -1,11 +0,0 @@
1
- import { NormalizedResult, TransformParams } from '../types';
2
- import { StorageAdapter } from '../storage/interface';
3
- import { SchemaValidator } from './validator';
4
- export declare class NormalizationEngine {
5
- private readonly storage;
6
- private readonly validator;
7
- constructor(storage: StorageAdapter, validator?: SchemaValidator);
8
- transform(params: TransformParams): Promise<NormalizedResult>;
9
- private extractValue;
10
- private applyTransform;
11
- }
@@ -1,86 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NormalizationEngine = void 0;
4
- const registry_1 = require("../templates/registry");
5
- const validator_1 = require("./validator");
6
- class NormalizationEngine {
7
- constructor(storage, validator = new validator_1.SchemaValidator()) {
8
- this.storage = storage;
9
- this.validator = validator;
10
- }
11
- async transform(params) {
12
- const { rawPayload, provider, schemaId } = params;
13
- const schema = await this.storage.getSchema(schemaId);
14
- if (!schema)
15
- throw new Error(`Schema not found: ${schemaId}`);
16
- const baseTemplate = await this.storage.getBaseTemplate(schema.baseTemplateId) || registry_1.templateRegistry.getById(schema.baseTemplateId);
17
- if (!baseTemplate)
18
- throw new Error(`Base template not found: ${schema.baseTemplateId}`);
19
- const validation = this.validator.validateSchema(schema, baseTemplate);
20
- if (!validation.valid) {
21
- throw new Error(`Invalid schema: ${validation.errors.join('; ')}`);
22
- }
23
- const providerMapping = schema.providerMappings.find((m) => m.provider === provider);
24
- if (!providerMapping)
25
- throw new Error(`No mapping found for provider: ${provider}`);
26
- const normalized = {};
27
- for (const field of schema.fields) {
28
- if (!field.enabled)
29
- continue;
30
- const mapping = providerMapping.fieldMappings.find((m) => m.schemaFieldId === field.id);
31
- if (mapping) {
32
- const value = this.extractValue(rawPayload, mapping.providerPath);
33
- const finalValue = this.applyTransform(value, mapping.transform);
34
- normalized[field.name] = finalValue ?? field.defaultValue;
35
- }
36
- else if (field.required) {
37
- if (field.defaultValue !== undefined) {
38
- normalized[field.name] = field.defaultValue;
39
- }
40
- else {
41
- throw new Error(`Required field ${field.name} has no mapping`);
42
- }
43
- }
44
- }
45
- const outValidation = this.validator.validateOutput(normalized, schema, baseTemplate);
46
- if (!outValidation.valid) {
47
- throw new Error(`Normalized output invalid: ${outValidation.errors.join('; ')}`);
48
- }
49
- return {
50
- normalized,
51
- meta: {
52
- provider,
53
- schemaId,
54
- schemaVersion: schema.baseTemplateId,
55
- transformedAt: new Date(),
56
- },
57
- };
58
- }
59
- extractValue(obj, path) {
60
- if (!path)
61
- return undefined;
62
- return path.split('.').reduce((acc, key) => (acc == null ? undefined : acc[key]), obj);
63
- }
64
- applyTransform(value, transform) {
65
- if (transform == null)
66
- return value;
67
- if (value == null)
68
- return value;
69
- if (transform === 'toUpperCase')
70
- return String(value).toUpperCase();
71
- if (transform === 'toLowerCase')
72
- return String(value).toLowerCase();
73
- if (transform === 'toNumber')
74
- return typeof value === 'number' ? value : Number(value);
75
- if (transform.startsWith('divide:')) {
76
- const denominator = Number(transform.split(':')[1]);
77
- return Number(value) / denominator;
78
- }
79
- if (transform.startsWith('multiply:')) {
80
- const factor = Number(transform.split(':')[1]);
81
- return Number(value) * factor;
82
- }
83
- return value;
84
- }
85
- }
86
- exports.NormalizationEngine = NormalizationEngine;
@@ -1,12 +0,0 @@
1
- import { BaseTemplate, UserSchema } from '../types';
2
- export declare class SchemaValidator {
3
- validateSchema(userSchema: UserSchema, baseTemplate: BaseTemplate): {
4
- valid: boolean;
5
- errors: string[];
6
- };
7
- validateOutput(output: Record<string, unknown>, userSchema: UserSchema, baseTemplate: BaseTemplate): {
8
- valid: boolean;
9
- errors: string[];
10
- };
11
- private matchesType;
12
- }
@@ -1,56 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SchemaValidator = void 0;
4
- class SchemaValidator {
5
- validateSchema(userSchema, baseTemplate) {
6
- const errors = [];
7
- // Ensure required base fields exist and are enabled or have defaults
8
- for (const baseField of baseTemplate.fields) {
9
- if (!baseField.required)
10
- continue;
11
- const userField = userSchema.fields.find((f) => f.id === baseField.id);
12
- if (!userField) {
13
- errors.push(`Missing required field in schema: ${baseField.id}`);
14
- continue;
15
- }
16
- if (!userField.enabled && baseField.defaultValue === undefined) {
17
- errors.push(`Required field disabled without default: ${baseField.id}`);
18
- }
19
- if (userField.type !== baseField.type) {
20
- errors.push(`Type mismatch for field ${baseField.id}: expected ${baseField.type}, got ${userField.type}`);
21
- }
22
- }
23
- return { valid: errors.length === 0, errors };
24
- }
25
- validateOutput(output, userSchema, baseTemplate) {
26
- const errors = [];
27
- for (const field of userSchema.fields) {
28
- if (!field.enabled)
29
- continue;
30
- const value = output[field.name];
31
- if (value === undefined) {
32
- if (field.required)
33
- errors.push(`Missing required field in output: ${field.name}`);
34
- continue;
35
- }
36
- if (!this.matchesType(value, field.type)) {
37
- errors.push(`Type mismatch for output field ${field.name}`);
38
- }
39
- }
40
- return { valid: errors.length === 0, errors };
41
- }
42
- matchesType(value, type) {
43
- if (type === 'number')
44
- return typeof value === 'number' && !Number.isNaN(value);
45
- if (type === 'string')
46
- return typeof value === 'string';
47
- if (type === 'boolean')
48
- return typeof value === 'boolean';
49
- if (type === 'object')
50
- return typeof value === 'object' && value !== null && !Array.isArray(value);
51
- if (type === 'array')
52
- return Array.isArray(value);
53
- return true;
54
- }
55
- }
56
- exports.SchemaValidator = SchemaValidator;
@@ -1,79 +0,0 @@
1
- export type TemplateCategory = 'payment' | 'auth' | 'ecommerce';
2
- export interface TemplateField {
3
- id: string;
4
- name: string;
5
- type: 'string' | 'number' | 'boolean' | 'object' | 'array';
6
- required: boolean;
7
- description?: string;
8
- defaultValue?: unknown;
9
- }
10
- export interface BaseTemplate {
11
- id: string;
12
- category: TemplateCategory;
13
- version: string;
14
- fields: TemplateField[];
15
- }
16
- export interface UserSchemaField {
17
- id: string;
18
- name: string;
19
- type: TemplateField['type'];
20
- required: boolean;
21
- enabled: boolean;
22
- defaultValue?: unknown;
23
- }
24
- export interface FieldMapping {
25
- schemaFieldId: string;
26
- providerPath: string;
27
- transform?: string;
28
- }
29
- export interface ProviderMapping {
30
- provider: string;
31
- fieldMappings: FieldMapping[];
32
- }
33
- export interface UserSchema {
34
- id: string;
35
- userId: string;
36
- baseTemplateId: string;
37
- category: TemplateCategory;
38
- fields: UserSchemaField[];
39
- providerMappings: ProviderMapping[];
40
- createdAt: Date;
41
- updatedAt: Date;
42
- }
43
- export interface NormalizedPayloadMeta {
44
- provider: string;
45
- schemaId: string;
46
- schemaVersion: string;
47
- transformedAt: Date;
48
- }
49
- export interface NormalizedResult {
50
- normalized: Record<string, unknown>;
51
- meta: NormalizedPayloadMeta;
52
- }
53
- export interface CreateSchemaInput {
54
- userId: string;
55
- baseTemplateId: string;
56
- category: TemplateCategory;
57
- fields: UserSchemaField[];
58
- providerMappings: ProviderMapping[];
59
- }
60
- export interface UpdateSchemaInput {
61
- fields?: UserSchemaField[];
62
- providerMappings?: ProviderMapping[];
63
- }
64
- export interface ProviderInfoField {
65
- path: string;
66
- type?: TemplateField['type'];
67
- description?: string;
68
- }
69
- export interface ProviderInfo {
70
- id: string;
71
- name: string;
72
- category: TemplateCategory;
73
- samplePaths?: ProviderInfoField[];
74
- }
75
- export interface TransformParams {
76
- rawPayload: unknown;
77
- provider: string;
78
- schemaId: string;
79
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });