@clivly/auth-better-auth 0.24.0-next.1 → 0.25.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/dist/index.cjs CHANGED
@@ -8,6 +8,56 @@ function betterAuthUserToClivlyUser(user) {
8
8
  name: user.name ?? void 0
9
9
  };
10
10
  }
11
+ // src/instant-push.ts
12
+ const MAX_HOLD_MS = 2e3;
13
+ /** `work`, but never longer than MAX_HOLD_MS. */
14
+ async function heldBriefly(work) {
15
+ let timer;
16
+ const timeout = new Promise((resolve) => {
17
+ timer = setTimeout(resolve, MAX_HOLD_MS);
18
+ });
19
+ await Promise.race([work, timeout]);
20
+ clearTimeout(timer);
21
+ }
22
+ /**
23
+ * better-auth plugin: a new, changed or deleted user reaches Clivly right
24
+ * away. Runs after better-auth has written the row, and never fails the
25
+ * sign-up: a push that does not arrive is left to the next sync.
26
+ */
27
+ function clivlyInstantPush(options) {
28
+ const { clivly, waitUntil, entity = "user" } = options;
29
+ const run = async (push, context) => {
30
+ const work = (async () => {
31
+ try {
32
+ await push();
33
+ } catch (error) {
34
+ console.warn(`Clivly instant push failed for "${entity}": ${error instanceof Error ? error.message : String(error)}. It will still sync on the next delta run.`);
35
+ }
36
+ })();
37
+ try {
38
+ const background = waitUntil ?? context?.context?.runInBackground;
39
+ if (background) {
40
+ background(work);
41
+ return;
42
+ }
43
+ } catch {}
44
+ await heldBriefly(work);
45
+ };
46
+ const changed = async (user, context) => {
47
+ if (user?.id) await run(() => clivly.pushById(entity, [user.id]), context);
48
+ };
49
+ const deleted = async (user, context) => {
50
+ if (user?.id) await run(() => clivly.pushDelete(entity, [user.id]), context);
51
+ };
52
+ return {
53
+ id: "clivly-instant-push",
54
+ init: () => ({ options: { databaseHooks: { user: {
55
+ create: { after: changed },
56
+ update: { after: changed },
57
+ delete: { after: deleted }
58
+ } } } })
59
+ };
60
+ }
11
61
  // src/index.ts
12
62
  /**
13
63
  * better-auth → ClivlyAuthAdapter. Resolves the CRM end-user's identity from the
@@ -23,4 +73,5 @@ function createBetterAuthAdapter(options) {
23
73
  } };
24
74
  }
25
75
  exports.betterAuthUserToClivlyUser = betterAuthUserToClivlyUser;
76
+ exports.clivlyInstantPush = clivlyInstantPush;
26
77
  exports.createBetterAuthAdapter = createBetterAuthAdapter;
package/dist/index.d.cts CHANGED
@@ -5,6 +5,56 @@ interface BetterAuthLikeUser {
5
5
  name?: string | null;
6
6
  }
7
7
  export declare function betterAuthUserToClivlyUser(user: BetterAuthLikeUser): ClivlyUser | null;
8
+ /** The two SDK calls the plugin makes; a `createClivlySDK` result satisfies it. */
9
+ interface InstantPushSDK {
10
+ pushById(entity: string, ids: readonly (string | number)[]): Promise<unknown>;
11
+ pushDelete(entity: string, ids: readonly (string | number)[]): Promise<unknown>;
12
+ }
13
+ interface ClivlyInstantPushOptions {
14
+ /** Your Clivly SDK (the object returned by `createClivlySDK({...})`). */
15
+ clivly: InstantPushSDK;
16
+ /** The entity key in your Clivly config that maps users. Default `"user"`. */
17
+ entity?: string;
18
+ /**
19
+ * Keeps the runtime alive after the response (Vercel `waitUntil`, Workers
20
+ * `ctx.waitUntil`). Without it, better-auth's own background runner is used
21
+ * when configured; without either, a sign-up waits up to two seconds.
22
+ */
23
+ waitUntil?: (work: Promise<void>) => void;
24
+ }
25
+ interface HookUser {
26
+ id: string;
27
+ }
28
+ interface HookContext {
29
+ context?: {
30
+ runInBackground?: (work: Promise<void>) => void;
31
+ };
32
+ }
33
+ /**
34
+ * better-auth plugin: a new, changed or deleted user reaches Clivly right
35
+ * away. Runs after better-auth has written the row, and never fails the
36
+ * sign-up: a push that does not arrive is left to the next sync.
37
+ */
38
+ export declare function clivlyInstantPush(options: ClivlyInstantPushOptions): {
39
+ id: "clivly-instant-push";
40
+ init: () => {
41
+ options: {
42
+ databaseHooks: {
43
+ user: {
44
+ create: {
45
+ after: (user: HookUser | null | undefined, context?: HookContext | null) => Promise<void>;
46
+ };
47
+ update: {
48
+ after: (user: HookUser | null | undefined, context?: HookContext | null) => Promise<void>;
49
+ };
50
+ delete: {
51
+ after: (user: HookUser | null | undefined, context?: HookContext | null) => Promise<void>;
52
+ };
53
+ };
54
+ };
55
+ };
56
+ };
57
+ };
8
58
  export interface BetterAuthLike {
9
59
  api: {
10
60
  getSession(args: {
@@ -25,4 +75,4 @@ export interface BetterAuthAdapterOptions {
25
75
  * `crm_members` layer.
26
76
  */
27
77
  export declare function createBetterAuthAdapter(options: BetterAuthAdapterOptions): ClivlyAuthAdapter;
28
- export type { BetterAuthLikeUser };
78
+ export type { BetterAuthLikeUser, ClivlyInstantPushOptions, InstantPushSDK };
package/dist/index.d.mts CHANGED
@@ -5,6 +5,56 @@ interface BetterAuthLikeUser {
5
5
  name?: string | null;
6
6
  }
7
7
  export declare function betterAuthUserToClivlyUser(user: BetterAuthLikeUser): ClivlyUser | null;
8
+ /** The two SDK calls the plugin makes; a `createClivlySDK` result satisfies it. */
9
+ interface InstantPushSDK {
10
+ pushById(entity: string, ids: readonly (string | number)[]): Promise<unknown>;
11
+ pushDelete(entity: string, ids: readonly (string | number)[]): Promise<unknown>;
12
+ }
13
+ interface ClivlyInstantPushOptions {
14
+ /** Your Clivly SDK (the object returned by `createClivlySDK({...})`). */
15
+ clivly: InstantPushSDK;
16
+ /** The entity key in your Clivly config that maps users. Default `"user"`. */
17
+ entity?: string;
18
+ /**
19
+ * Keeps the runtime alive after the response (Vercel `waitUntil`, Workers
20
+ * `ctx.waitUntil`). Without it, better-auth's own background runner is used
21
+ * when configured; without either, a sign-up waits up to two seconds.
22
+ */
23
+ waitUntil?: (work: Promise<void>) => void;
24
+ }
25
+ interface HookUser {
26
+ id: string;
27
+ }
28
+ interface HookContext {
29
+ context?: {
30
+ runInBackground?: (work: Promise<void>) => void;
31
+ };
32
+ }
33
+ /**
34
+ * better-auth plugin: a new, changed or deleted user reaches Clivly right
35
+ * away. Runs after better-auth has written the row, and never fails the
36
+ * sign-up: a push that does not arrive is left to the next sync.
37
+ */
38
+ export declare function clivlyInstantPush(options: ClivlyInstantPushOptions): {
39
+ id: "clivly-instant-push";
40
+ init: () => {
41
+ options: {
42
+ databaseHooks: {
43
+ user: {
44
+ create: {
45
+ after: (user: HookUser | null | undefined, context?: HookContext | null) => Promise<void>;
46
+ };
47
+ update: {
48
+ after: (user: HookUser | null | undefined, context?: HookContext | null) => Promise<void>;
49
+ };
50
+ delete: {
51
+ after: (user: HookUser | null | undefined, context?: HookContext | null) => Promise<void>;
52
+ };
53
+ };
54
+ };
55
+ };
56
+ };
57
+ };
8
58
  export interface BetterAuthLike {
9
59
  api: {
10
60
  getSession(args: {
@@ -25,4 +75,4 @@ export interface BetterAuthAdapterOptions {
25
75
  * `crm_members` layer.
26
76
  */
27
77
  export declare function createBetterAuthAdapter(options: BetterAuthAdapterOptions): ClivlyAuthAdapter;
28
- export type { BetterAuthLikeUser };
78
+ export type { BetterAuthLikeUser, ClivlyInstantPushOptions, InstantPushSDK };
package/dist/index.mjs CHANGED
@@ -7,6 +7,56 @@ function betterAuthUserToClivlyUser(user) {
7
7
  name: user.name ?? void 0
8
8
  };
9
9
  }
10
+ // src/instant-push.ts
11
+ const MAX_HOLD_MS = 2e3;
12
+ /** `work`, but never longer than MAX_HOLD_MS. */
13
+ async function heldBriefly(work) {
14
+ let timer;
15
+ const timeout = new Promise((resolve) => {
16
+ timer = setTimeout(resolve, MAX_HOLD_MS);
17
+ });
18
+ await Promise.race([work, timeout]);
19
+ clearTimeout(timer);
20
+ }
21
+ /**
22
+ * better-auth plugin: a new, changed or deleted user reaches Clivly right
23
+ * away. Runs after better-auth has written the row, and never fails the
24
+ * sign-up: a push that does not arrive is left to the next sync.
25
+ */
26
+ function clivlyInstantPush(options) {
27
+ const { clivly, waitUntil, entity = "user" } = options;
28
+ const run = async (push, context) => {
29
+ const work = (async () => {
30
+ try {
31
+ await push();
32
+ } catch (error) {
33
+ console.warn(`Clivly instant push failed for "${entity}": ${error instanceof Error ? error.message : String(error)}. It will still sync on the next delta run.`);
34
+ }
35
+ })();
36
+ try {
37
+ const background = waitUntil ?? context?.context?.runInBackground;
38
+ if (background) {
39
+ background(work);
40
+ return;
41
+ }
42
+ } catch {}
43
+ await heldBriefly(work);
44
+ };
45
+ const changed = async (user, context) => {
46
+ if (user?.id) await run(() => clivly.pushById(entity, [user.id]), context);
47
+ };
48
+ const deleted = async (user, context) => {
49
+ if (user?.id) await run(() => clivly.pushDelete(entity, [user.id]), context);
50
+ };
51
+ return {
52
+ id: "clivly-instant-push",
53
+ init: () => ({ options: { databaseHooks: { user: {
54
+ create: { after: changed },
55
+ update: { after: changed },
56
+ delete: { after: deleted }
57
+ } } } })
58
+ };
59
+ }
10
60
  // src/index.ts
11
61
  /**
12
62
  * better-auth → ClivlyAuthAdapter. Resolves the CRM end-user's identity from the
@@ -21,4 +71,4 @@ function createBetterAuthAdapter(options) {
21
71
  return betterAuthUserToClivlyUser(session.user);
22
72
  } };
23
73
  }
24
- export { betterAuthUserToClivlyUser, createBetterAuthAdapter };
74
+ export { betterAuthUserToClivlyUser, clivlyInstantPush, createBetterAuthAdapter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clivly/auth-better-auth",
3
- "version": "0.24.0-next.1",
3
+ "version": "0.25.0",
4
4
  "private": false,
5
5
  "description": "better-auth implementation of Clivly's ClivlyAuthAdapter.",
6
6
  "homepage": "https://clivly.com/docs",
@@ -29,7 +29,7 @@
29
29
  "README.md"
30
30
  ],
31
31
  "dependencies": {
32
- "@clivly/core": "0.24.0-next.1"
32
+ "@clivly/core": "0.25.0"
33
33
  },
34
34
  "devDependencies": {
35
35
  "tsdown": "^0.23.7",