@clikgate/node 2.0.0 → 2.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.
Files changed (86) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +126 -6
  3. package/SECURITY.md +2 -1
  4. package/dist/cjs/client.d.ts +54 -10
  5. package/dist/cjs/client.js +75 -16
  6. package/dist/cjs/currency.d.ts +7 -0
  7. package/dist/cjs/currency.js +75 -0
  8. package/dist/cjs/customer-id.d.ts +26 -0
  9. package/dist/cjs/customer-id.js +52 -0
  10. package/dist/cjs/enums.js +1 -1
  11. package/dist/cjs/errors.d.ts +1 -1
  12. package/dist/cjs/event-types.d.ts +108 -0
  13. package/dist/cjs/event-types.js +109 -0
  14. package/dist/cjs/express.d.ts +11 -2
  15. package/dist/cjs/express.js +3 -2
  16. package/dist/cjs/fastify.d.ts +4 -1
  17. package/dist/cjs/fastify.js +3 -2
  18. package/dist/cjs/generated/index.d.ts +2015 -83
  19. package/dist/cjs/generated/index.js +83 -3
  20. package/dist/cjs/identity.d.ts +12 -2
  21. package/dist/cjs/identity.js +37 -3
  22. package/dist/cjs/index.d.ts +11 -1
  23. package/dist/cjs/index.js +21 -1
  24. package/dist/cjs/installation.d.ts +47 -0
  25. package/dist/cjs/installation.js +83 -0
  26. package/dist/cjs/known-customers.d.ts +45 -0
  27. package/dist/cjs/known-customers.js +95 -0
  28. package/dist/cjs/lookups.d.ts +128 -0
  29. package/dist/cjs/lookups.js +149 -0
  30. package/dist/cjs/models.d.ts +342 -0
  31. package/dist/cjs/models.js +2 -0
  32. package/dist/cjs/next.d.ts +22 -0
  33. package/dist/cjs/next.js +20 -0
  34. package/dist/cjs/pager.d.ts +5 -0
  35. package/dist/cjs/pager.js +40 -0
  36. package/dist/cjs/referrals.d.ts +169 -0
  37. package/dist/cjs/referrals.js +208 -0
  38. package/dist/cjs/resources.d.ts +34 -5
  39. package/dist/cjs/resources.js +43 -42
  40. package/dist/cjs/types.d.ts +57 -10
  41. package/dist/cjs/version.d.ts +1 -1
  42. package/dist/cjs/version.js +1 -1
  43. package/dist/cjs/webhooks.d.ts +3 -2
  44. package/dist/cjs/webhooks.js +43 -2
  45. package/dist/esm/client.d.ts +54 -10
  46. package/dist/esm/client.js +75 -16
  47. package/dist/esm/currency.d.ts +7 -0
  48. package/dist/esm/currency.js +67 -0
  49. package/dist/esm/customer-id.d.ts +26 -0
  50. package/dist/esm/customer-id.js +47 -0
  51. package/dist/esm/enums.js +1 -1
  52. package/dist/esm/errors.d.ts +1 -1
  53. package/dist/esm/event-types.d.ts +108 -0
  54. package/dist/esm/event-types.js +106 -0
  55. package/dist/esm/express.d.ts +11 -2
  56. package/dist/esm/express.js +3 -2
  57. package/dist/esm/fastify.d.ts +4 -1
  58. package/dist/esm/fastify.js +3 -2
  59. package/dist/esm/generated/index.d.ts +2015 -83
  60. package/dist/esm/generated/index.js +80 -1
  61. package/dist/esm/identity.d.ts +12 -2
  62. package/dist/esm/identity.js +36 -3
  63. package/dist/esm/index.d.ts +11 -1
  64. package/dist/esm/index.js +6 -0
  65. package/dist/esm/installation.d.ts +47 -0
  66. package/dist/esm/installation.js +77 -0
  67. package/dist/esm/known-customers.d.ts +45 -0
  68. package/dist/esm/known-customers.js +90 -0
  69. package/dist/esm/lookups.d.ts +128 -0
  70. package/dist/esm/lookups.js +142 -0
  71. package/dist/esm/models.d.ts +342 -0
  72. package/dist/esm/models.js +1 -0
  73. package/dist/esm/next.d.ts +22 -0
  74. package/dist/esm/next.js +19 -0
  75. package/dist/esm/pager.d.ts +5 -0
  76. package/dist/esm/pager.js +36 -0
  77. package/dist/esm/referrals.d.ts +169 -0
  78. package/dist/esm/referrals.js +204 -0
  79. package/dist/esm/resources.d.ts +34 -5
  80. package/dist/esm/resources.js +43 -42
  81. package/dist/esm/types.d.ts +57 -10
  82. package/dist/esm/version.d.ts +1 -1
  83. package/dist/esm/version.js +1 -1
  84. package/dist/esm/webhooks.d.ts +3 -2
  85. package/dist/esm/webhooks.js +42 -2
  86. package/package.json +2 -2
@@ -0,0 +1,36 @@
1
+ /** @internal Iterate pages: `fetch(cursor)` returns the page after `cursor` (`null` for the first). */
2
+ export function paginate(fetch) {
3
+ async function* iterate() {
4
+ let cursor = null;
5
+ for (;;) {
6
+ const page = await fetch(cursor);
7
+ for (const item of page.data ?? [])
8
+ yield item;
9
+ if (!page.has_more || !page.next_cursor)
10
+ return;
11
+ cursor = page.next_cursor;
12
+ }
13
+ }
14
+ return {
15
+ [Symbol.asyncIterator]: iterate,
16
+ async toArray(max = Number.POSITIVE_INFINITY) {
17
+ const out = [];
18
+ if (max <= 0)
19
+ return out;
20
+ for await (const item of iterate()) {
21
+ out.push(item);
22
+ if (out.length >= max)
23
+ break;
24
+ }
25
+ return out;
26
+ },
27
+ };
28
+ }
29
+ /** @internal Copy `params` without `undefined` values. */
30
+ export function defined(params) {
31
+ const out = {};
32
+ for (const [k, v] of Object.entries(params))
33
+ if (v !== undefined)
34
+ out[k] = v;
35
+ return out;
36
+ }
@@ -0,0 +1,169 @@
1
+ import { type RequestOptions } from './http.js';
2
+ import { type MoneyInput } from './money.js';
3
+ import { type PageIterator } from './pager.js';
4
+ import type { Job, Referral, Referrer, ReferrerResult } from './models.js';
5
+ import type { ListPage } from './types.js';
6
+ /** Fields of `referrals.getOrCreateReferrer`. */
7
+ export interface ReferrerCreateParams {
8
+ /** Your customer ID (sent as `customer_user_id`). */
9
+ customerId: string;
10
+ displayName?: string;
11
+ /** A code the customer already had in your own system. */
12
+ externalCode?: string;
13
+ }
14
+ /** Look a referrer up by your customer ID or by their code (exactly one). */
15
+ export type ReferrerLookup = {
16
+ customerId: string;
17
+ code?: undefined;
18
+ } | {
19
+ code: string;
20
+ customerId?: undefined;
21
+ };
22
+ /** Filters of `referrals.list` / `listPage`. */
23
+ export interface ReferralListParams {
24
+ programId?: string;
25
+ referrerId?: string;
26
+ /** The referee's customer ID (sent as `referee_customer_user_id`). */
27
+ refereeCustomerId?: string;
28
+ status?: string;
29
+ from?: Date | string;
30
+ to?: Date | string;
31
+ /** 1-100 per page. */
32
+ limit?: number;
33
+ startingAfter?: string;
34
+ endingBefore?: string;
35
+ }
36
+ /** Fields of `referrals.completeStep`. */
37
+ export interface CompleteStepParams {
38
+ /** The step's goal key. */
39
+ goal: string;
40
+ /** When the step happened (required by the API). */
41
+ occurredAt: Date | string;
42
+ /** Normal units (SPEC section 8). */
43
+ value?: MoneyInput;
44
+ currency?: string;
45
+ }
46
+ /** One row of `referrals.importReferrers`. */
47
+ export interface ReferrerImportRow {
48
+ customerId: string;
49
+ externalCode?: string;
50
+ displayName?: string;
51
+ }
52
+ /** Fields of `referrals.updateReferrer`. */
53
+ export interface ReferrerUpdateParams {
54
+ displayName?: string | null;
55
+ /** Your own free-form data about the referrer. */
56
+ attributes?: Record<string, unknown> | null;
57
+ }
58
+ /** The API's limit of referrers per import request. */
59
+ export declare const REFERRER_IMPORT_BATCH = 1000;
60
+ /** Referrals: referrers, referrals, manual overrides, qualification steps and imports. */
61
+ export declare class Referrals {
62
+ private readonly core;
63
+ private call;
64
+ /**
65
+ * Get the referrer for one of your customers in a program, creating it (with its code and link) the first time.
66
+ * `created` is true when it was just created.
67
+ *
68
+ * @example
69
+ * const referrer = await clikgate.referrals.getOrCreateReferrer('rpg_01J…', { customerId: user.id, displayName: user.firstName });
70
+ * console.log(referrer.code, referrer.url, referrer.created);
71
+ */
72
+ getOrCreateReferrer(programId: string, params: ReferrerCreateParams, requestOptions?: RequestOptions): Promise<ReferrerResult>;
73
+ /**
74
+ * Fetch a referrer by ID.
75
+ *
76
+ * @example
77
+ * const referrer = await clikgate.referrals.getReferrer('rfr_01J…');
78
+ */
79
+ getReferrer(referrerId: string, requestOptions?: RequestOptions): Promise<Referrer>;
80
+ /**
81
+ * Find a referrer by your customer ID or by code. Returns `null` when there is none (404); other errors throw.
82
+ *
83
+ * @example
84
+ * const referrer = await clikgate.referrals.findReferrer('rpg_01J…', { customerId: user.id });
85
+ * if (!referrer) { … }
86
+ */
87
+ findReferrer(programId: string, lookup: ReferrerLookup, requestOptions?: RequestOptions): Promise<Referrer | null>;
88
+ /**
89
+ * Fetch a referral by ID. Reward sides carry `amount` (normal units) next to `amount_minor`.
90
+ *
91
+ * @example
92
+ * const referral = await clikgate.referrals.get('ref_01J…');
93
+ * console.log(referral.status, referral.reward_snapshot?.referrer?.amount);
94
+ */
95
+ get(referralId: string, requestOptions?: RequestOptions): Promise<Referral>;
96
+ /**
97
+ * Fetch one page of referrals.
98
+ *
99
+ * @example
100
+ * const page = await clikgate.referrals.listPage({ programId: 'rpg_01J…', status: 'qualified', limit: 100 });
101
+ */
102
+ listPage(params?: ReferralListParams, requestOptions?: RequestOptions): Promise<ListPage<Referral>>;
103
+ /**
104
+ * Iterate over every referral, fetching pages lazily.
105
+ *
106
+ * @example
107
+ * for await (const referral of clikgate.referrals.list({ programId: 'rpg_01J…', status: 'qualified' })) {
108
+ * console.log(referral.id, referral.referee.customer_user_id);
109
+ * }
110
+ */
111
+ list(params?: ReferralListParams, requestOptions?: RequestOptions): PageIterator<Referral>;
112
+ /**
113
+ * Qualify a referral by hand (a manual override), with your reason.
114
+ *
115
+ * @example
116
+ * await clikgate.referrals.qualify('ref_01J…', { reason: 'Checked by support' });
117
+ */
118
+ qualify(referralId: string, params: {
119
+ reason: string;
120
+ }, requestOptions?: RequestOptions): Promise<Referral>;
121
+ /**
122
+ * Disqualify a referral by hand (a manual override), with your reason.
123
+ *
124
+ * @example
125
+ * await clikgate.referrals.disqualify('ref_01J…', { reason: 'Same household' });
126
+ */
127
+ disqualify(referralId: string, params: {
128
+ reason: string;
129
+ }, requestOptions?: RequestOptions): Promise<Referral>;
130
+ private override;
131
+ /**
132
+ * Complete the referral's current qualification step from your server (a goal it can't see otherwise).
133
+ *
134
+ * @example
135
+ * await clikgate.referrals.completeStep('ref_01J…', { goal: 'first_order', occurredAt: order.paidAt, value: '1500.50', currency: 'NGN' });
136
+ */
137
+ completeStep(referralId: string, params: CompleteStepParams, requestOptions?: RequestOptions): Promise<Referral>;
138
+ /**
139
+ * Import referrers (customers who already refer others, with their old codes). Rows are sent in batches of 1000;
140
+ * the result is one job per batch.
141
+ *
142
+ * @example
143
+ * const jobs = await clikgate.referrals.importReferrers('rpg_01J…', [{ customerId: 'user-42', externalCode: 'ADA-OLD' }]);
144
+ */
145
+ importReferrers(programId: string, rows: ReferrerImportRow[], requestOptions?: RequestOptions): Promise<Job[]>;
146
+ /**
147
+ * Block a referrer: their code and link stop creating referrals.
148
+ *
149
+ * @example
150
+ * await clikgate.referrals.blockReferrer('rfr_01J…', { reason: 'Fake installs' });
151
+ */
152
+ blockReferrer(referrerId: string, params?: {
153
+ reason?: string;
154
+ }, requestOptions?: RequestOptions): Promise<Referrer>;
155
+ /**
156
+ * Unblock a referrer.
157
+ *
158
+ * @example
159
+ * await clikgate.referrals.unblockReferrer('rfr_01J…');
160
+ */
161
+ unblockReferrer(referrerId: string, requestOptions?: RequestOptions): Promise<Referrer>;
162
+ /**
163
+ * Update a referrer's display name or your own attributes (`null` clears).
164
+ *
165
+ * @example
166
+ * await clikgate.referrals.updateReferrer('rfr_01J…', { displayName: 'Ada O.', attributes: { tier: 'gold' } });
167
+ */
168
+ updateReferrer(referrerId: string, params: ReferrerUpdateParams, requestOptions?: RequestOptions): Promise<Referrer>;
169
+ }
@@ -0,0 +1,204 @@
1
+ import { NotFoundError, localError } from './errors.js';
2
+ import { encodePathSegment, formatDate, requireId } from './http.js';
3
+ import { formatValue } from './money.js';
4
+ import { toReferral, toReferrer, toReferrerResult } from './currency.js';
5
+ import { defined, paginate } from './pager.js';
6
+ /** The API's limit of referrers per import request. */
7
+ export const REFERRER_IMPORT_BATCH = 1000;
8
+ /** Referrals: referrers, referrals, manual overrides, qualification steps and imports. */
9
+ export class Referrals {
10
+ core;
11
+ /** @internal */
12
+ constructor(core) {
13
+ this.core = core;
14
+ }
15
+ async call(method, path, init, requestOptions) {
16
+ const { status, data } = await this.core.requester.request({ method, path, ...init, ...requestOptions });
17
+ return { status, data };
18
+ }
19
+ /**
20
+ * Get the referrer for one of your customers in a program, creating it (with its code and link) the first time.
21
+ * `created` is true when it was just created.
22
+ *
23
+ * @example
24
+ * const referrer = await clikgate.referrals.getOrCreateReferrer('rpg_01J…', { customerId: user.id, displayName: user.firstName });
25
+ * console.log(referrer.code, referrer.url, referrer.created);
26
+ */
27
+ async getOrCreateReferrer(programId, params, requestOptions = {}) {
28
+ const program = encodePathSegment(requireId(programId, 'program_id'));
29
+ const body = defined({ customer_user_id: requireId(params?.customerId, 'customer_id'), display_name: params.displayName, external_code: params.externalCode });
30
+ const { status, data } = await this.call('POST', `/v1/referral-programs/${program}/referrers`, { body }, requestOptions);
31
+ return toReferrerResult(data, status);
32
+ }
33
+ /**
34
+ * Fetch a referrer by ID.
35
+ *
36
+ * @example
37
+ * const referrer = await clikgate.referrals.getReferrer('rfr_01J…');
38
+ */
39
+ async getReferrer(referrerId, requestOptions = {}) {
40
+ const { data } = await this.call('GET', `/v1/referrers/${encodePathSegment(requireId(referrerId, 'referrer_id'))}`, {}, requestOptions);
41
+ return toReferrer(data);
42
+ }
43
+ /**
44
+ * Find a referrer by your customer ID or by code. Returns `null` when there is none (404); other errors throw.
45
+ *
46
+ * @example
47
+ * const referrer = await clikgate.referrals.findReferrer('rpg_01J…', { customerId: user.id });
48
+ * if (!referrer) { … }
49
+ */
50
+ async findReferrer(programId, lookup, requestOptions = {}) {
51
+ const program = encodePathSegment(requireId(programId, 'program_id'));
52
+ const hasCustomer = lookup?.customerId !== undefined && lookup?.customerId !== null;
53
+ const hasCode = lookup?.code !== undefined && lookup?.code !== null;
54
+ if (hasCustomer === hasCode)
55
+ throw localError('invalid_argument', 'Pass exactly one of customerId and code.', 'customer_id');
56
+ const path = hasCustomer
57
+ ? `/v1/referral-programs/${program}/referrers/by-user/${encodePathSegment(requireId(lookup.customerId, 'customer_id'))}`
58
+ : `/v1/referral-programs/${program}/referrers/by-code/${encodePathSegment(requireId(lookup.code, 'code'))}`;
59
+ try {
60
+ const { data } = await this.call('GET', path, {}, requestOptions);
61
+ return toReferrer(data);
62
+ }
63
+ catch (err) {
64
+ if (err instanceof NotFoundError)
65
+ return null;
66
+ throw err;
67
+ }
68
+ }
69
+ /**
70
+ * Fetch a referral by ID. Reward sides carry `amount` (normal units) next to `amount_minor`.
71
+ *
72
+ * @example
73
+ * const referral = await clikgate.referrals.get('ref_01J…');
74
+ * console.log(referral.status, referral.reward_snapshot?.referrer?.amount);
75
+ */
76
+ async get(referralId, requestOptions = {}) {
77
+ const { data } = await this.call('GET', `/v1/referrals/${encodePathSegment(requireId(referralId, 'referral_id'))}`, {}, requestOptions);
78
+ return toReferral(data);
79
+ }
80
+ /**
81
+ * Fetch one page of referrals.
82
+ *
83
+ * @example
84
+ * const page = await clikgate.referrals.listPage({ programId: 'rpg_01J…', status: 'qualified', limit: 100 });
85
+ */
86
+ async listPage(params = {}, requestOptions = {}) {
87
+ const query = defined({
88
+ program_id: params.programId,
89
+ referrer_id: params.referrerId,
90
+ referee_customer_user_id: params.refereeCustomerId,
91
+ status: params.status,
92
+ from: params.from,
93
+ to: params.to,
94
+ limit: params.limit,
95
+ starting_after: params.startingAfter,
96
+ ending_before: params.endingBefore,
97
+ });
98
+ const { data } = await this.call('GET', '/v1/referrals', { query }, requestOptions);
99
+ return { ...data, data: (data?.data ?? []).map(toReferral) };
100
+ }
101
+ /**
102
+ * Iterate over every referral, fetching pages lazily.
103
+ *
104
+ * @example
105
+ * for await (const referral of clikgate.referrals.list({ programId: 'rpg_01J…', status: 'qualified' })) {
106
+ * console.log(referral.id, referral.referee.customer_user_id);
107
+ * }
108
+ */
109
+ list(params = {}, requestOptions = {}) {
110
+ return paginate((cursor) => this.listPage(cursor ? { ...params, startingAfter: cursor } : params, requestOptions));
111
+ }
112
+ /**
113
+ * Qualify a referral by hand (a manual override), with your reason.
114
+ *
115
+ * @example
116
+ * await clikgate.referrals.qualify('ref_01J…', { reason: 'Checked by support' });
117
+ */
118
+ async qualify(referralId, params, requestOptions = {}) {
119
+ return this.override(referralId, 'qualify', params?.reason, requestOptions);
120
+ }
121
+ /**
122
+ * Disqualify a referral by hand (a manual override), with your reason.
123
+ *
124
+ * @example
125
+ * await clikgate.referrals.disqualify('ref_01J…', { reason: 'Same household' });
126
+ */
127
+ async disqualify(referralId, params, requestOptions = {}) {
128
+ return this.override(referralId, 'disqualify', params?.reason, requestOptions);
129
+ }
130
+ async override(referralId, action, reason, requestOptions) {
131
+ const path = `/v1/referrals/${encodePathSegment(requireId(referralId, 'referral_id'))}/override`;
132
+ const { data } = await this.call('POST', path, { body: { action, reason } }, requestOptions);
133
+ return toReferral(data);
134
+ }
135
+ /**
136
+ * Complete the referral's current qualification step from your server (a goal it can't see otherwise).
137
+ *
138
+ * @example
139
+ * await clikgate.referrals.completeStep('ref_01J…', { goal: 'first_order', occurredAt: order.paidAt, value: '1500.50', currency: 'NGN' });
140
+ */
141
+ async completeStep(referralId, params, requestOptions = {}) {
142
+ const path = `/v1/referrals/${encodePathSegment(requireId(referralId, 'referral_id'))}/steps`;
143
+ if (params?.occurredAt === undefined || params.occurredAt === null || params.occurredAt === '')
144
+ throw localError('invalid_argument', 'occurredAt is required: when the step happened.', 'occurred_at');
145
+ const value = params.value === undefined || params.value === null ? undefined : formatValue(params.value, (m) => this.core.warn('inexact_number', m));
146
+ const body = defined({ goal: params.goal, occurred_at: formatDate(params.occurredAt), value, currency: params.currency });
147
+ const { data } = await this.call('POST', path, { body }, requestOptions);
148
+ return toReferral(data);
149
+ }
150
+ /**
151
+ * Import referrers (customers who already refer others, with their old codes). Rows are sent in batches of 1000;
152
+ * the result is one job per batch.
153
+ *
154
+ * @example
155
+ * const jobs = await clikgate.referrals.importReferrers('rpg_01J…', [{ customerId: 'user-42', externalCode: 'ADA-OLD' }]);
156
+ */
157
+ async importReferrers(programId, rows, requestOptions = {}) {
158
+ const program = encodePathSegment(requireId(programId, 'program_id'));
159
+ if (!Array.isArray(rows))
160
+ throw localError('invalid_argument', 'rows must be a list.', 'rows');
161
+ const wire = rows.map((r, i) => defined({ customer_user_id: requireId(r?.customerId, `rows[${i}].customer_id`), external_code: r.externalCode, display_name: r.displayName }));
162
+ const jobs = [];
163
+ const batches = Math.ceil(wire.length / REFERRER_IMPORT_BATCH);
164
+ for (let b = 0; b < batches; b++) {
165
+ const key = requestOptions.idempotencyKey && batches > 1 ? `${requestOptions.idempotencyKey}-${b}` : requestOptions.idempotencyKey;
166
+ const body = { referrers: wire.slice(b * REFERRER_IMPORT_BATCH, (b + 1) * REFERRER_IMPORT_BATCH) };
167
+ const { data } = await this.call('POST', `/v1/referral-programs/${program}/referrers/import`, { body }, { ...requestOptions, idempotencyKey: key });
168
+ jobs.push(data);
169
+ }
170
+ return jobs;
171
+ }
172
+ /**
173
+ * Block a referrer: their code and link stop creating referrals.
174
+ *
175
+ * @example
176
+ * await clikgate.referrals.blockReferrer('rfr_01J…', { reason: 'Fake installs' });
177
+ */
178
+ async blockReferrer(referrerId, params = {}, requestOptions = {}) {
179
+ const path = `/v1/referrers/${encodePathSegment(requireId(referrerId, 'referrer_id'))}/block`;
180
+ const { data } = await this.call('POST', path, { body: defined({ reason: params?.reason }) }, requestOptions);
181
+ return toReferrer(data);
182
+ }
183
+ /**
184
+ * Unblock a referrer.
185
+ *
186
+ * @example
187
+ * await clikgate.referrals.unblockReferrer('rfr_01J…');
188
+ */
189
+ async unblockReferrer(referrerId, requestOptions = {}) {
190
+ const { data } = await this.call('POST', `/v1/referrers/${encodePathSegment(requireId(referrerId, 'referrer_id'))}/unblock`, {}, requestOptions);
191
+ return toReferrer(data);
192
+ }
193
+ /**
194
+ * Update a referrer's display name or your own attributes (`null` clears).
195
+ *
196
+ * @example
197
+ * await clikgate.referrals.updateReferrer('rfr_01J…', { displayName: 'Ada O.', attributes: { tier: 'gold' } });
198
+ */
199
+ async updateReferrer(referrerId, params, requestOptions = {}) {
200
+ const body = defined({ display_name: params?.displayName, attributes: params?.attributes });
201
+ const { data } = await this.call('PATCH', `/v1/referrers/${encodePathSegment(requireId(referrerId, 'referrer_id'))}`, { body }, requestOptions);
202
+ return toReferrer(data);
203
+ }
204
+ }
@@ -1,5 +1,7 @@
1
1
  import { type RequestOptions } from './http.js';
2
2
  import type { Click, Conversion, GoalEvent, Link, LinkUpdateResult, ListPage, QrCode } from './types.js';
3
+ import type { LinkCreateParams, LinkUpdateParams } from './models.js';
4
+ import { type PageIterator } from './pager.js';
3
5
  type Params = Record<string, unknown>;
4
6
  /** Conversions: the older API. New integrations should use goals. */
5
7
  export declare class Conversions {
@@ -36,11 +38,8 @@ export interface QrOptions {
36
38
  dark?: string;
37
39
  light?: string;
38
40
  }
39
- /** An async iterator over every link that also exposes the first page. */
40
- export interface LinkIterator extends AsyncIterable<Link> {
41
- /** Collect up to `max` links (default: all). */
42
- toArray(max?: number): Promise<Link[]>;
43
- }
41
+ /** An async iterator over every link. */
42
+ export type LinkIterator = PageIterator<Link>;
44
43
  /** Links: create, read, update (with approvals and locks), list and QR codes. */
45
44
  export declare class Links {
46
45
  private readonly core;
@@ -51,6 +50,7 @@ export declare class Links {
51
50
  * const link = await clikgate.links.create({ destination_url: 'https://example.com/spring', og_title: 'Spring sale', og_image_url: 'https://example.com/og.png' });
52
51
  * console.log(link.short_url, link.warnings);
53
52
  */
53
+ create(params: LinkCreateParams, requestOptions?: RequestOptions): Promise<Link>;
54
54
  create(params: Params, requestOptions?: RequestOptions): Promise<Link>;
55
55
  /**
56
56
  * Fetch a link by ID.
@@ -68,6 +68,7 @@ export declare class Links {
68
68
  * const result = await clikgate.links.update('lnk_01J…', { destination_url: 'https://example.com/summer' });
69
69
  * if (!result.applied) console.log('waiting for approval:', result.change_request.id);
70
70
  */
71
+ update(id: string, params: LinkUpdateParams, requestOptions?: RequestOptions): Promise<LinkUpdateResult>;
71
72
  update(id: string, params: Params, requestOptions?: RequestOptions): Promise<LinkUpdateResult>;
72
73
  /**
73
74
  * Fetch one page of links (`limit` 1–100, `starting_after`, `ending_before`, filters such as `status`, `tag`, `campaign_id`).
@@ -85,6 +86,34 @@ export declare class Links {
85
86
  * }
86
87
  */
87
88
  list(params?: Params, requestOptions?: RequestOptions): LinkIterator;
89
+ /**
90
+ * Archive a link (`DELETE /v1/links/{id}`): it stops redirecting. Returns the archived link.
91
+ *
92
+ * @example
93
+ * const link = await clikgate.links.archive('lnk_01J…');
94
+ */
95
+ archive(id: string, requestOptions?: RequestOptions): Promise<Link>;
96
+ /**
97
+ * Fetch a link by your own external reference (`external_reference`).
98
+ *
99
+ * @example
100
+ * const link = await clikgate.links.getByReference('order-1001');
101
+ */
102
+ getByReference(reference: string, requestOptions?: RequestOptions): Promise<Link>;
103
+ /**
104
+ * Pause a link: visitors see the paused page until you resume it.
105
+ *
106
+ * @example
107
+ * const link = await clikgate.links.pause('lnk_01J…');
108
+ */
109
+ pause(id: string, requestOptions?: RequestOptions): Promise<Link>;
110
+ /**
111
+ * Resume a paused link.
112
+ *
113
+ * @example
114
+ * const link = await clikgate.links.resume('lnk_01J…');
115
+ */
116
+ resume(id: string, requestOptions?: RequestOptions): Promise<Link>;
88
117
  /**
89
118
  * Download the link's QR code.
90
119
  *
@@ -2,6 +2,7 @@ import { guardPayload } from './guard.js';
2
2
  import { encodePathSegment, requireId } from './http.js';
3
3
  import { formatValue } from './money.js';
4
4
  import { checkHashedFields } from './ads-hashing.js';
5
+ import { paginate } from './pager.js';
5
6
  function withFormattedValue(core, params) {
6
7
  const out = {};
7
8
  for (const [k, v] of Object.entries(params))
@@ -62,13 +63,6 @@ export class Links {
62
63
  constructor(core) {
63
64
  this.core = core;
64
65
  }
65
- /**
66
- * Create a link. The result's `warnings` include `no_preview` when there is no preview title and image.
67
- *
68
- * @example
69
- * const link = await clikgate.links.create({ destination_url: 'https://example.com/spring', og_title: 'Spring sale', og_image_url: 'https://example.com/og.png' });
70
- * console.log(link.short_url, link.warnings);
71
- */
72
66
  async create(params, requestOptions = {}) {
73
67
  const { data } = await this.core.requester.request({ method: 'POST', path: '/v1/links', body: params, ...requestOptions });
74
68
  return data;
@@ -83,15 +77,6 @@ export class Links {
83
77
  const { data } = await this.core.requester.request({ method: 'GET', path: `/v1/links/${encodePathSegment(requireId(id))}`, ...requestOptions });
84
78
  return data;
85
79
  }
86
- /**
87
- * Update a link. With two-person approval on, a destination change answers `202` and waits for a
88
- * second person: the result is `{ applied: false, change_request }`. A locked link throws {@link LinkLockedError}.
89
- * An explicit `null` clears a field.
90
- *
91
- * @example
92
- * const result = await clikgate.links.update('lnk_01J…', { destination_url: 'https://example.com/summer' });
93
- * if (!result.applied) console.log('waiting for approval:', result.change_request.id);
94
- */
95
80
  async update(id, params, requestOptions = {}) {
96
81
  const { status, data } = await this.core.requester.request({
97
82
  method: 'PATCH',
@@ -123,32 +108,48 @@ export class Links {
123
108
  * }
124
109
  */
125
110
  list(params = {}, requestOptions = {}) {
126
- const listPage = (p) => this.listPage(p, requestOptions);
127
- async function* iterate() {
128
- let cursor = null;
129
- for (;;) {
130
- const page = await listPage(cursor ? { ...params, starting_after: cursor } : params);
131
- for (const item of page.data ?? [])
132
- yield item;
133
- if (!page.has_more || !page.next_cursor)
134
- return;
135
- cursor = page.next_cursor;
136
- }
137
- }
138
- return {
139
- [Symbol.asyncIterator]: iterate,
140
- async toArray(max = Number.POSITIVE_INFINITY) {
141
- const out = [];
142
- if (max <= 0)
143
- return out;
144
- for await (const l of iterate()) {
145
- out.push(l);
146
- if (out.length >= max)
147
- break;
148
- }
149
- return out;
150
- },
151
- };
111
+ return paginate((cursor) => this.listPage(cursor ? { ...params, starting_after: cursor } : params, requestOptions));
112
+ }
113
+ /**
114
+ * Archive a link (`DELETE /v1/links/{id}`): it stops redirecting. Returns the archived link.
115
+ *
116
+ * @example
117
+ * const link = await clikgate.links.archive('lnk_01J…');
118
+ */
119
+ async archive(id, requestOptions = {}) {
120
+ const { data } = await this.core.requester.request({ method: 'DELETE', path: `/v1/links/${encodePathSegment(requireId(id))}`, ...requestOptions });
121
+ return data;
122
+ }
123
+ /**
124
+ * Fetch a link by your own external reference (`external_reference`).
125
+ *
126
+ * @example
127
+ * const link = await clikgate.links.getByReference('order-1001');
128
+ */
129
+ async getByReference(reference, requestOptions = {}) {
130
+ const path = `/v1/links/by-reference/${encodePathSegment(requireId(reference, 'reference'))}`;
131
+ const { data } = await this.core.requester.request({ method: 'GET', path, ...requestOptions });
132
+ return data;
133
+ }
134
+ /**
135
+ * Pause a link: visitors see the paused page until you resume it.
136
+ *
137
+ * @example
138
+ * const link = await clikgate.links.pause('lnk_01J…');
139
+ */
140
+ async pause(id, requestOptions = {}) {
141
+ const { data } = await this.core.requester.request({ method: 'POST', path: `/v1/links/${encodePathSegment(requireId(id))}/pause`, ...requestOptions });
142
+ return data;
143
+ }
144
+ /**
145
+ * Resume a paused link.
146
+ *
147
+ * @example
148
+ * const link = await clikgate.links.resume('lnk_01J…');
149
+ */
150
+ async resume(id, requestOptions = {}) {
151
+ const { data } = await this.core.requester.request({ method: 'POST', path: `/v1/links/${encodePathSegment(requireId(id))}/resume`, ...requestOptions });
152
+ return data;
152
153
  }
153
154
  /**
154
155
  * Download the link's QR code.