@superlayer/webhooks 1.0.67

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,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const core_1 = require("@superlayer/core");
4
+ const index_ts_1 = require("../index.js");
5
+ // ---------- With a schema ----------
6
+ const schema = core_1.i.schema({
7
+ entities: {
8
+ users: core_1.i.entity({
9
+ email: core_1.i.string().indexed(),
10
+ age: core_1.i.number().optional(),
11
+ }),
12
+ },
13
+ });
14
+ function _typedRecordWithSchemaCreate() {
15
+ const { typedHandlers } = index_ts_1.Webhooks.helpers();
16
+ typedHandlers('users', 'create', (record) => {
17
+ });
18
+ }
19
+ function _typedRecordWithSchemaUpdate() {
20
+ const { typedHandlers } = index_ts_1.Webhooks.helpers();
21
+ typedHandlers('users', 'update', (record) => {
22
+ });
23
+ }
24
+ function _typedRecordWithSchemaDelete() {
25
+ const { typedHandlers } = index_ts_1.Webhooks.helpers();
26
+ typedHandlers('users', 'delete', (record) => {
27
+ });
28
+ }
29
+ function _defaultHandlerWithSchema() {
30
+ const { typedHandlers } = index_ts_1.Webhooks.helpers();
31
+ typedHandlers('$default', (record) => {
32
+ });
33
+ }
34
+ // ---------- Without a schema ----------
35
+ function _typedRecordNoSchemaCreate() {
36
+ // Helpers default to InstantUnknownSchema when no schema is given.
37
+ const { typedHandlers } = index_ts_1.Webhooks.helpers();
38
+ typedHandlers('$users', 'create', (record) => {
39
+ // Arbitrary field access is allowed under unknown schema (would error
40
+ // if `after` resolved to `never`).
41
+ const _email = record.after.email;
42
+ });
43
+ }
44
+ function _typedRecordNoSchemaUpdate() {
45
+ const { typedHandlers } = index_ts_1.Webhooks.helpers();
46
+ typedHandlers('posts', 'update', (record) => {
47
+ });
48
+ }
49
+ function _typedRecordNoSchemaDelete() {
50
+ const { typedHandlers } = index_ts_1.Webhooks.helpers();
51
+ typedHandlers('comments', 'delete', (record) => {
52
+ });
53
+ }
54
+ function _defaultHandlerNoSchema() {
55
+ const { typedHandlers } = index_ts_1.Webhooks.helpers();
56
+ typedHandlers('$default', (record) => {
57
+ });
58
+ }
59
+ // ---------- combineHandlers preserves narrowing ----------
60
+ function _combineHandlersAcceptsTypedEntries() {
61
+ const { typedHandlers, combineHandlers } = index_ts_1.Webhooks.helpers();
62
+ const handlers = combineHandlers(typedHandlers('users', 'create', (_record) => { }), typedHandlers('users', 'update', (_record) => { }), typedHandlers('$default', (_record) => { }));
63
+ }
64
+ function _combineHandlersAcceptsTypedEntriesNoSchema() {
65
+ // Same call shape should work against an unknown schema.
66
+ const { typedHandlers, combineHandlers } = index_ts_1.Webhooks.helpers();
67
+ const handlers = combineHandlers(typedHandlers('$users', 'create', (_record) => { }), typedHandlers('$default', (_record) => { }));
68
+ }
69
+ // ---------- Unknown namespace is rejected when a schema is supplied ----------
70
+ function _typedHandlersRejectsUnknownNamespaceWithSchema() {
71
+ const { typedHandlers } = index_ts_1.Webhooks.helpers();
72
+ // The schema only declares `users`. Passing a namespace not in the schema
73
+ // should fail typecheck.
74
+ // @ts-expect-error - 'posts' is not in Schema['entities']
75
+ typedHandlers('posts', 'create', (_record) => { });
76
+ // Sanity: the known namespace still typechecks.
77
+ typedHandlers('users', 'create', (_record) => { });
78
+ }
79
+ //# sourceMappingURL=typesTests.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typesTests.js","sourceRoot":"","sources":["../../../src/__types__/typesTests.ts"],"names":[],"mappings":";;AAAA,2CAAqC;AACrC,0CAAuC;AAGvC,sCAAsC;AAEtC,MAAM,MAAM,GAAG,QAAC,CAAC,MAAM,CAAC;IACtB,QAAQ,EAAE;QACR,KAAK,EAAE,QAAC,CAAC,MAAM,CAAC;YACd,KAAK,EAAE,QAAC,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE;YAC3B,GAAG,EAAE,QAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;SAC3B,CAAC;KACH;C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AE,GAAG,mBAAQ,CAAC,OAAO,EAAU,CAAC;IACrD,0EAA0E;IAC1E,yBAAyB;IACzB,0DAA0D;IAC1D,aAAa,CAAC,OAAO,EAAE,QAAQ,EAAE,CAAC,OAAO,EAAE,EAAE,GAAE,CAAC,CAAC,CAAC;IAElD,gDAAgD;IAChD,aAAa,CAAC,OAAO,EAAE,QAAQ,EAAE,CAAC,OAAO,EAAE,EAAE,GAAE,CAAC,CAAC,CAAC;AACpD,CAAC","sourcesContent":["import { i } from '@superlayer/core';\nimport { Webhooks } from '../index.ts';\nimport type { Equal, Expect, IsAny, NotAny, NotNever } from './typeUtils.ts';\n\n// ---------- With a schema ----------\n\nconst schema = i.schema({\n entities: {\n users: i.entity({\n email: i.string().indexed(),\n age: i.number().optional(),\n }),\n },\n});\n\ntype Schema = typeof schema;\n\nfunction _typedRecordWithSchemaCreate() {\n const { typedHandlers } = Webhooks.helpers<Schema>();\n typedHandlers('users', 'create', (record) => {\n type Namespace = typeof record.namespace;\n type Action = typeof record.action;\n type Before = typeof record.before;\n type After = typeof record.after;\n\n type _cases = [\n Expect<NotAny<typeof record>>,\n Expect<Equal<Namespace, 'users'>>,\n Expect<Equal<Action, 'create'>>,\n Expect<Equal<Before, null>>,\n Expect<NotAny<After>>,\n // The entity has id + the schema attrs.\n Expect<Equal<After['id'], string>>,\n Expect<Equal<After['email'], string>>,\n Expect<Equal<After['age'], number | undefined>>,\n ];\n });\n}\n\nfunction _typedRecordWithSchemaUpdate() {\n const { typedHandlers } = Webhooks.helpers<Schema>();\n typedHandlers('users', 'update', (record) => {\n type Before = typeof record.before;\n type After = typeof record.after;\n type _cases = [\n Expect<Equal<typeof record.namespace, 'users'>>,\n Expect<Equal<typeof record.action, 'update'>>,\n // Both before and after are present for updates.\n Expect<Equal<Before['id'], string>>,\n Expect<Equal<After['id'], string>>,\n Expect<Equal<Before['email'], string>>,\n Expect<Equal<After['email'], string>>,\n ];\n });\n}\n\nfunction _typedRecordWithSchemaDelete() {\n const { typedHandlers } = Webhooks.helpers<Schema>();\n typedHandlers('users', 'delete', (record) => {\n type _cases = [\n Expect<Equal<typeof record.namespace, 'users'>>,\n Expect<Equal<typeof record.action, 'delete'>>,\n // `after` is null for deletes; `before` carries the entity.\n Expect<Equal<typeof record.after, null>>,\n Expect<Equal<typeof record.before.id, string>>,\n Expect<Equal<typeof record.before.email, string>>,\n ];\n });\n}\n\nfunction _defaultHandlerWithSchema() {\n const { typedHandlers } = Webhooks.helpers<Schema>();\n typedHandlers('$default', (record) => {\n type Namespace = typeof record.namespace;\n type Action = typeof record.action;\n type _cases = [\n // The $default form spans all namespaces and actions in the schema.\n Expect<Equal<Namespace, 'users'>>,\n Expect<Equal<Action, 'create' | 'update' | 'delete'>>,\n ];\n });\n}\n\n// ---------- Without a schema ----------\n\nfunction _typedRecordNoSchemaCreate() {\n // Helpers default to InstantUnknownSchema when no schema is given.\n const { typedHandlers } = Webhooks.helpers();\n typedHandlers('$users', 'create', (record) => {\n type Namespace = typeof record.namespace;\n type Action = typeof record.action;\n type After = typeof record.after;\n type _cases = [\n Expect<NotAny<typeof record>>,\n Expect<NotNever<typeof record>>,\n // The literal namespace passed at the call site is preserved even without\n // a schema — this is the regression we just fixed.\n Expect<Equal<Namespace, '$users'>>,\n Expect<Equal<Action, 'create'>>,\n Expect<Equal<typeof record.before, null>>,\n // `after` is an entity reachable without a schema (the regression we\n // fixed). With unknown attrs, individual fields fall through to `any`\n // via the index signature, so we just check the shape is usable.\n Expect<NotNever<After>>,\n Expect<NotNever<After['id']>>,\n ];\n // Arbitrary field access is allowed under unknown schema (would error\n // if `after` resolved to `never`).\n const _email: unknown = record.after.email;\n });\n}\n\nfunction _typedRecordNoSchemaUpdate() {\n const { typedHandlers } = Webhooks.helpers();\n typedHandlers('posts', 'update', (record) => {\n type _cases = [\n Expect<Equal<typeof record.namespace, 'posts'>>,\n Expect<Equal<typeof record.action, 'update'>>,\n Expect<NotNever<typeof record.before>>,\n Expect<NotNever<typeof record.after>>,\n ];\n });\n}\n\nfunction _typedRecordNoSchemaDelete() {\n const { typedHandlers } = Webhooks.helpers();\n typedHandlers('comments', 'delete', (record) => {\n type _cases = [\n Expect<Equal<typeof record.namespace, 'comments'>>,\n Expect<Equal<typeof record.action, 'delete'>>,\n Expect<Equal<typeof record.after, null>>,\n Expect<NotNever<typeof record.before>>,\n ];\n });\n}\n\nfunction _defaultHandlerNoSchema() {\n const { typedHandlers } = Webhooks.helpers();\n typedHandlers('$default', (record) => {\n type Namespace = typeof record.namespace;\n type Action = typeof record.action;\n type _cases = [\n // Without a schema, namespace is exactly `string` — not `string | number`\n // (which would leak the TS index-signature `keyof` artifact, since\n // namespaces are always strings on the wire).\n Expect<Equal<Namespace, string>>,\n Expect<Equal<Action, 'create' | 'update' | 'delete'>>,\n ];\n });\n}\n\n// ---------- combineHandlers preserves narrowing ----------\n\nfunction _combineHandlersAcceptsTypedEntries() {\n const { typedHandlers, combineHandlers } = Webhooks.helpers<Schema>();\n const handlers = combineHandlers(\n typedHandlers('users', 'create', (_record) => {}),\n typedHandlers('users', 'update', (_record) => {}),\n typedHandlers('$default', (_record) => {}),\n );\n type _cases = [Expect<NotAny<typeof handlers>>];\n}\n\nfunction _combineHandlersAcceptsTypedEntriesNoSchema() {\n // Same call shape should work against an unknown schema.\n const { typedHandlers, combineHandlers } = Webhooks.helpers();\n const handlers = combineHandlers(\n typedHandlers('$users', 'create', (_record) => {}),\n typedHandlers('$default', (_record) => {}),\n );\n type _cases = [Expect<NotAny<typeof handlers>>];\n}\n\n// ---------- Unknown namespace is rejected when a schema is supplied ----------\n\nfunction _typedHandlersRejectsUnknownNamespaceWithSchema() {\n const { typedHandlers } = Webhooks.helpers<Schema>();\n // The schema only declares `users`. Passing a namespace not in the schema\n // should fail typecheck.\n // @ts-expect-error - 'posts' is not in Schema['entities']\n typedHandlers('posts', 'create', (_record) => {});\n\n // Sanity: the known namespace still typechecks.\n typedHandlers('users', 'create', (_record) => {});\n}\n\n// Silence unused-symbol warnings for the test cases.\ntype _silence = IsAny<unknown>;\n"]}