@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
@@ -12,7 +12,7 @@
12
12
  * }
13
13
  */
14
14
  /** Error codes with documented meaning (SPEC section 5). Other codes can appear; `code` is a plain string. */
15
- export type KnownErrorCode = 'invalid_goal_key' | 'invalid_reference' | 'invalid_value' | 'click_id_and_code' | 'batch_too_large' | 'batch_empty' | 'invalid_argument' | 'invalid_click_id' | 'queue_full' | 'client_closed' | 'missing_api_key' | 'invalid_api_key' | 'invalid_option' | 'network_error' | 'timeout' | 'unexpected_status' | 'rate_limit_exceeded' | 'private_mode_field_rejected' | 'link_locked' | 'idempotency_key_reused' | 'idempotency_request_in_progress' | 'goal_archived' | 'goal_not_found' | 'value_required' | 'value_not_allowed' | 'domain_mode_mismatch' | 'https_required' | 'destination_not_allowlisted' | 'test_destination_not_allowed' | 'test_link_limit_reached' | 'plan_limit_reached' | 'plan_upgrade_required' | 'plan_feature_unavailable' | 'feature_disabled' | 'invalid_provider' | 'unknown_provider' | 'code_not_supported' | 'payment_goal_needs_confirmation' | 'unknown_goal' | 'cli_scope' | 'missing_signature' | 'malformed_signature' | 'timestamp_outside_tolerance' | 'no_matching_signature' | 'invalid_payload' | 'server_only' | 'invalid_customer_id' | 'missing_identity_secret' | 'invalid_identity_secrets';
15
+ export type KnownErrorCode = 'invalid_goal_key' | 'invalid_reference' | 'invalid_value' | 'click_id_and_code' | 'batch_too_large' | 'batch_empty' | 'invalid_argument' | 'invalid_click_id' | 'queue_full' | 'client_closed' | 'missing_api_key' | 'invalid_api_key' | 'invalid_option' | 'network_error' | 'timeout' | 'unexpected_status' | 'rate_limit_exceeded' | 'private_mode_field_rejected' | 'private_mode_conflict' | 'link_locked' | 'idempotency_key_reused' | 'idempotency_request_in_progress' | 'goal_archived' | 'goal_not_found' | 'value_required' | 'value_not_allowed' | 'domain_mode_mismatch' | 'https_required' | 'destination_not_allowlisted' | 'test_destination_not_allowed' | 'test_link_limit_reached' | 'plan_limit_reached' | 'plan_upgrade_required' | 'plan_feature_unavailable' | 'feature_disabled' | 'invalid_provider' | 'unknown_provider' | 'code_not_supported' | 'payment_goal_needs_confirmation' | 'unknown_goal' | 'cli_scope' | 'missing_signature' | 'malformed_signature' | 'timestamp_outside_tolerance' | 'no_matching_signature' | 'invalid_payload' | 'server_only' | 'invalid_customer_id' | 'missing_identity_secret' | 'invalid_identity_secrets';
16
16
  /** Fields every Clikgate error carries. */
17
17
  export interface ClikgateErrorInit {
18
18
  message: string;
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Every webhook event type the API sends (SPEC 29.1), as constants holding the wire string.
3
+ * `event.type` stays a plain string, so a type added later is never an error.
4
+ *
5
+ * @example
6
+ * if (event.type === EventType.ReferralQualified) { … }
7
+ */
8
+ export declare const EventType: {
9
+ readonly AbTestLikelyWinner: "ab_test.likely_winner";
10
+ readonly AlertResolved: "alert.resolved";
11
+ readonly AlertTriggered: "alert.triggered";
12
+ readonly AppPushCredentialsInvalid: "app.push_credentials_invalid";
13
+ readonly AppReengaged: "app.reengaged";
14
+ readonly AppBannerPaused: "app_banner.paused";
15
+ readonly AppBannerPublished: "app_banner.published";
16
+ readonly CampaignPaused: "campaign.paused";
17
+ readonly CampaignResumed: "campaign.resumed";
18
+ readonly ChallengeCancelled: "challenge.cancelled";
19
+ readonly ChallengeEnded: "challenge.ended";
20
+ readonly ChallengeFinalized: "challenge.finalized";
21
+ readonly ChallengeScheduled: "challenge.scheduled";
22
+ readonly ChallengeStarted: "challenge.started";
23
+ readonly ClickCreated: "click.created";
24
+ readonly CodeRedeemed: "code.redeemed";
25
+ readonly ConversionAttributed: "conversion.attributed";
26
+ readonly ConversionReversed: "conversion.reversed";
27
+ readonly ConversionUnattributed: "conversion.unattributed";
28
+ readonly CreditNoteIssued: "credit_note.issued";
29
+ readonly DomainGroupChanged: "domain.group_changed";
30
+ readonly DomainSettingsUpdated: "domain.settings_updated";
31
+ readonly DomainStatusChanged: "domain.status_changed";
32
+ readonly FraudInstallFlagged: "fraud.install_flagged";
33
+ readonly FunnelStuck: "funnel.stuck";
34
+ readonly GoalCompleted: "goal.completed";
35
+ readonly GoalReversed: "goal.reversed";
36
+ readonly IdentityDuplicateDetected: "identity.duplicate_detected";
37
+ readonly InstallAttributed: "install.attributed";
38
+ readonly InstallAttributionUpdated: "install.attribution_updated";
39
+ readonly InstallOrganic: "install.organic";
40
+ readonly InstallUninstallReverted: "install.uninstall_reverted";
41
+ readonly InstallUninstalled: "install.uninstalled";
42
+ readonly InvoiceIssued: "invoice.issued";
43
+ readonly InvoicePaid: "invoice.paid";
44
+ readonly InvoiceVoided: "invoice.voided";
45
+ readonly JobCompleted: "job.completed";
46
+ readonly LeaderboardPeriodClosed: "leaderboard.period_closed";
47
+ readonly LeaderboardPeriodFinalized: "leaderboard.period_finalized";
48
+ readonly LeaderboardRankChanged: "leaderboard.rank_changed";
49
+ readonly LeaderboardTargetReached: "leaderboard.target_reached";
50
+ readonly LeaderboardTargetRevoked: "leaderboard.target_revoked";
51
+ readonly LinkArchived: "link.archived";
52
+ readonly LinkCreated: "link.created";
53
+ readonly LinkDestinationChanged: "link.destination_changed";
54
+ readonly LinkExpired: "link.expired";
55
+ readonly LinkFlagged: "link.flagged";
56
+ readonly LinkPaused: "link.paused";
57
+ readonly LinkResumed: "link.resumed";
58
+ readonly LinkStatsHourly: "link.stats.hourly";
59
+ readonly LinkUpdated: "link.updated";
60
+ readonly OrganizationDeleted: "organization.deleted";
61
+ readonly OrganizationFeatureChanged: "organization.feature_changed";
62
+ readonly OrganizationFeatureDataPurged: "organization.feature_data_purged";
63
+ readonly PartnerApproved: "partner.approved";
64
+ readonly PartnerEarningApproved: "partner_earning.approved";
65
+ readonly PartnerPayoutPaid: "partner_payout.paid";
66
+ readonly PartnerRewardApplied: "partner_reward.applied";
67
+ readonly PartnerRewardApproved: "partner_reward.approved";
68
+ readonly PartnerRewardRemoved: "partner_reward.removed";
69
+ readonly PartnerRewardReversed: "partner_reward.reversed";
70
+ readonly PaymentNotConfirmed: "payment.not_confirmed";
71
+ readonly PaymentRefunded: "payment.refunded";
72
+ readonly PaymentRejected: "payment.rejected";
73
+ readonly PaymentVerified: "payment.verified";
74
+ readonly PersonDeviceChanged: "person.device_changed";
75
+ readonly PrivacyRequestCompleted: "privacy.request_completed";
76
+ readonly ReceiptIssued: "receipt.issued";
77
+ readonly ReferralCreated: "referral.created";
78
+ readonly ReferralDisqualified: "referral.disqualified";
79
+ readonly ReferralHolding: "referral.holding";
80
+ readonly ReferralQualified: "referral.qualified";
81
+ readonly ReferralRefereeUninstalled: "referral.referee_uninstalled";
82
+ readonly ReferralRejected: "referral.rejected";
83
+ readonly ReferralStepCompleted: "referral.step_completed";
84
+ readonly ReferrerBlocked: "referrer.blocked";
85
+ readonly ReferrerCreated: "referrer.created";
86
+ readonly ReferrerQualityChanged: "referrer.quality_changed";
87
+ readonly ReferrerUnblocked: "referrer.unblocked";
88
+ readonly SaleMatched: "sale.matched";
89
+ readonly SaleRefunded: "sale.refunded";
90
+ readonly SkanPostbackReceived: "skan.postback_received";
91
+ readonly SubscriptionActivated: "subscription.activated";
92
+ readonly SubscriptionCancelled: "subscription.cancelled";
93
+ readonly SubscriptionChanged: "subscription.changed";
94
+ readonly SubscriptionExpired: "subscription.expired";
95
+ readonly SubscriptionGraceStarted: "subscription.grace_started";
96
+ readonly SubscriptionRenewed: "subscription.renewed";
97
+ readonly SubscriptionTrialConverted: "subscription.trial_converted";
98
+ readonly SubscriptionTrialEnded: "subscription.trial_ended";
99
+ readonly SubscriptionTrialExtended: "subscription.trial_extended";
100
+ readonly SubscriptionTrialStarted: "subscription.trial_started";
101
+ readonly SubscriptionUpdated: "subscription.updated";
102
+ readonly UsageThresholdReached: "usage.threshold_reached";
103
+ readonly WebhookTest: "webhook.test";
104
+ readonly WebhookEndpointDisabled: "webhook_endpoint.disabled";
105
+ readonly WhatsappConversationAttributed: "whatsapp.conversation_attributed";
106
+ };
107
+ /** A webhook event type this SDK release knows (`event.type` may also be a newer one). */
108
+ export type EventTypeValue = (typeof EventType)[keyof typeof EventType];
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Every webhook event type the API sends (SPEC 29.1), as constants holding the wire string.
3
+ * `event.type` stays a plain string, so a type added later is never an error.
4
+ *
5
+ * @example
6
+ * if (event.type === EventType.ReferralQualified) { … }
7
+ */
8
+ export const EventType = {
9
+ AbTestLikelyWinner: 'ab_test.likely_winner',
10
+ AlertResolved: 'alert.resolved',
11
+ AlertTriggered: 'alert.triggered',
12
+ AppPushCredentialsInvalid: 'app.push_credentials_invalid',
13
+ AppReengaged: 'app.reengaged',
14
+ AppBannerPaused: 'app_banner.paused',
15
+ AppBannerPublished: 'app_banner.published',
16
+ CampaignPaused: 'campaign.paused',
17
+ CampaignResumed: 'campaign.resumed',
18
+ ChallengeCancelled: 'challenge.cancelled',
19
+ ChallengeEnded: 'challenge.ended',
20
+ ChallengeFinalized: 'challenge.finalized',
21
+ ChallengeScheduled: 'challenge.scheduled',
22
+ ChallengeStarted: 'challenge.started',
23
+ ClickCreated: 'click.created',
24
+ CodeRedeemed: 'code.redeemed',
25
+ ConversionAttributed: 'conversion.attributed',
26
+ ConversionReversed: 'conversion.reversed',
27
+ ConversionUnattributed: 'conversion.unattributed',
28
+ CreditNoteIssued: 'credit_note.issued',
29
+ DomainGroupChanged: 'domain.group_changed',
30
+ DomainSettingsUpdated: 'domain.settings_updated',
31
+ DomainStatusChanged: 'domain.status_changed',
32
+ FraudInstallFlagged: 'fraud.install_flagged',
33
+ FunnelStuck: 'funnel.stuck',
34
+ GoalCompleted: 'goal.completed',
35
+ GoalReversed: 'goal.reversed',
36
+ IdentityDuplicateDetected: 'identity.duplicate_detected',
37
+ InstallAttributed: 'install.attributed',
38
+ InstallAttributionUpdated: 'install.attribution_updated',
39
+ InstallOrganic: 'install.organic',
40
+ InstallUninstallReverted: 'install.uninstall_reverted',
41
+ InstallUninstalled: 'install.uninstalled',
42
+ InvoiceIssued: 'invoice.issued',
43
+ InvoicePaid: 'invoice.paid',
44
+ InvoiceVoided: 'invoice.voided',
45
+ JobCompleted: 'job.completed',
46
+ LeaderboardPeriodClosed: 'leaderboard.period_closed',
47
+ LeaderboardPeriodFinalized: 'leaderboard.period_finalized',
48
+ LeaderboardRankChanged: 'leaderboard.rank_changed',
49
+ LeaderboardTargetReached: 'leaderboard.target_reached',
50
+ LeaderboardTargetRevoked: 'leaderboard.target_revoked',
51
+ LinkArchived: 'link.archived',
52
+ LinkCreated: 'link.created',
53
+ LinkDestinationChanged: 'link.destination_changed',
54
+ LinkExpired: 'link.expired',
55
+ LinkFlagged: 'link.flagged',
56
+ LinkPaused: 'link.paused',
57
+ LinkResumed: 'link.resumed',
58
+ LinkStatsHourly: 'link.stats.hourly',
59
+ LinkUpdated: 'link.updated',
60
+ OrganizationDeleted: 'organization.deleted',
61
+ OrganizationFeatureChanged: 'organization.feature_changed',
62
+ OrganizationFeatureDataPurged: 'organization.feature_data_purged',
63
+ PartnerApproved: 'partner.approved',
64
+ PartnerEarningApproved: 'partner_earning.approved',
65
+ PartnerPayoutPaid: 'partner_payout.paid',
66
+ PartnerRewardApplied: 'partner_reward.applied',
67
+ PartnerRewardApproved: 'partner_reward.approved',
68
+ PartnerRewardRemoved: 'partner_reward.removed',
69
+ PartnerRewardReversed: 'partner_reward.reversed',
70
+ PaymentNotConfirmed: 'payment.not_confirmed',
71
+ PaymentRefunded: 'payment.refunded',
72
+ PaymentRejected: 'payment.rejected',
73
+ PaymentVerified: 'payment.verified',
74
+ PersonDeviceChanged: 'person.device_changed',
75
+ PrivacyRequestCompleted: 'privacy.request_completed',
76
+ ReceiptIssued: 'receipt.issued',
77
+ ReferralCreated: 'referral.created',
78
+ ReferralDisqualified: 'referral.disqualified',
79
+ ReferralHolding: 'referral.holding',
80
+ ReferralQualified: 'referral.qualified',
81
+ ReferralRefereeUninstalled: 'referral.referee_uninstalled',
82
+ ReferralRejected: 'referral.rejected',
83
+ ReferralStepCompleted: 'referral.step_completed',
84
+ ReferrerBlocked: 'referrer.blocked',
85
+ ReferrerCreated: 'referrer.created',
86
+ ReferrerQualityChanged: 'referrer.quality_changed',
87
+ ReferrerUnblocked: 'referrer.unblocked',
88
+ SaleMatched: 'sale.matched',
89
+ SaleRefunded: 'sale.refunded',
90
+ SkanPostbackReceived: 'skan.postback_received',
91
+ SubscriptionActivated: 'subscription.activated',
92
+ SubscriptionCancelled: 'subscription.cancelled',
93
+ SubscriptionChanged: 'subscription.changed',
94
+ SubscriptionExpired: 'subscription.expired',
95
+ SubscriptionGraceStarted: 'subscription.grace_started',
96
+ SubscriptionRenewed: 'subscription.renewed',
97
+ SubscriptionTrialConverted: 'subscription.trial_converted',
98
+ SubscriptionTrialEnded: 'subscription.trial_ended',
99
+ SubscriptionTrialExtended: 'subscription.trial_extended',
100
+ SubscriptionTrialStarted: 'subscription.trial_started',
101
+ SubscriptionUpdated: 'subscription.updated',
102
+ UsageThresholdReached: 'usage.threshold_reached',
103
+ WebhookTest: 'webhook.test',
104
+ WebhookEndpointDisabled: 'webhook_endpoint.disabled',
105
+ WhatsappConversationAttributed: 'whatsapp.conversation_attributed',
106
+ };
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Express / Connect middleware: keeps the click ID (SPEC section 17).
2
+ * Express / Connect middleware: keeps the click ID and reads the app's `Clikgate-Installation` header (SPEC section 17).
3
3
  *
4
4
  * @example
5
5
  * import express from 'express';
@@ -12,16 +12,25 @@
12
12
  * app.post('/signup', (req, res) => {
13
13
  * const clickId = req.clikgate.clickId; // save it with the application
14
14
  * });
15
+ * app.post('/login', async (req, res) => {
16
+ * const user = await signIn(req.body); // your own sign-in check first
17
+ * if (req.clikgate.installation) {
18
+ * await clikgate.identifyUser({ installation: req.clikgate.installation, customerId: user.id });
19
+ * }
20
+ * });
15
21
  *
16
22
  * @packageDocumentation
17
23
  */
18
24
  import type { IncomingMessage, ServerResponse } from 'node:http';
19
25
  import { type PersistOptions } from './click-ids.js';
20
26
  import type { Clikgate } from './client.js';
27
+ import { type Installation } from './installation.js';
21
28
  /** What the middleware puts on the request. */
22
29
  export interface ClikgateRequestContext {
23
30
  /** The click ID from the query, the `_cg_<param>` cookie or the `Clikgate-Click-Id` header; `null` when there is none. */
24
31
  clickId: string | null;
32
+ /** The app installation from the `Clikgate-Installation` header; `null` when it is missing or invalid. */
33
+ installation: Installation | null;
25
34
  }
26
35
  declare global {
27
36
  namespace Express {
@@ -38,7 +47,7 @@ export interface ClikgateMiddlewareOptions extends PersistOptions {
38
47
  }
39
48
  type Next = (err?: unknown) => void;
40
49
  /**
41
- * Express/Connect middleware: sets `req.clikgate.clickId`, and stores a click ID that arrives in
50
+ * Express/Connect middleware: sets `req.clikgate.clickId` and `req.clikgate.installation`, and stores a click ID that arrives in
42
51
  * the query string in the first-party `_cg_<param>` cookie. Pass the client so the organisation's
43
52
  * click-ID parameter is used; without it, `cgid`.
44
53
  *
@@ -1,4 +1,5 @@
1
1
  import { clickIds as defaultClickIds } from './click-ids.js';
2
+ import { installationFromRequest } from './installation.js';
2
3
  /** @internal Shared by the add-ons: read, expose and (optionally) persist the click ID. */
3
4
  export function captureClickId(helpers, req, res, options) {
4
5
  const clickId = helpers.fromRequest(req);
@@ -12,7 +13,7 @@ export function captureClickId(helpers, req, res, options) {
12
13
  return clickId;
13
14
  }
14
15
  /**
15
- * Express/Connect middleware: sets `req.clikgate.clickId`, and stores a click ID that arrives in
16
+ * Express/Connect middleware: sets `req.clikgate.clickId` and `req.clikgate.installation`, and stores a click ID that arrives in
16
17
  * the query string in the first-party `_cg_<param>` cookie. Pass the client so the organisation's
17
18
  * click-ID parameter is used; without it, `cgid`.
18
19
  *
@@ -30,7 +31,7 @@ export function clikgateMiddleware(client, options = {}) {
30
31
  void client.config().catch(() => undefined);
31
32
  }
32
33
  const clickId = captureClickId(helpers, req, res, options);
33
- req.clikgate = { clickId };
34
+ req.clikgate = { clickId, installation: installationFromRequest(req) };
34
35
  next();
35
36
  }
36
37
  catch (err) {
@@ -18,10 +18,13 @@
18
18
  import type { FastifyPluginAsync } from 'fastify';
19
19
  import { type PersistOptions } from './click-ids.js';
20
20
  import type { Clikgate } from './client.js';
21
+ import { type Installation } from './installation.js';
21
22
  /** What the plugin puts on the request. */
22
23
  export interface ClikgateRequestContext {
23
24
  /** The click ID, or `null`. */
24
25
  clickId: string | null;
26
+ /** The app installation from the `Clikgate-Installation` header; `null` when it is missing or invalid. */
27
+ installation: Installation | null;
25
28
  }
26
29
  declare module 'fastify' {
27
30
  interface FastifyRequest {
@@ -37,7 +40,7 @@ export interface ClikgatePluginOptions extends PersistOptions {
37
40
  persist?: boolean;
38
41
  }
39
42
  /**
40
- * Fastify plugin: sets `request.clikgate.clickId` and keeps a click ID from the query string in the
43
+ * Fastify plugin: sets `request.clikgate.clickId` and `request.clikgate.installation`, and keeps a click ID from the query string in the
41
44
  * `_cg_<param>` cookie.
42
45
  *
43
46
  * @example
@@ -1,4 +1,5 @@
1
1
  import { clickIds as defaultClickIds } from './click-ids.js';
2
+ import { installationFromRequest } from './installation.js';
2
3
  const plugin = async (fastify, options) => {
3
4
  const { client, persist, ...cookie } = options;
4
5
  const helpers = client ? client.clickIds : defaultClickIds;
@@ -8,7 +9,7 @@ const plugin = async (fastify, options) => {
8
9
  fastify.decorateRequest('clikgate', null);
9
10
  fastify.addHook('onRequest', async (request, reply) => {
10
11
  const clickId = helpers.fromRequest({ url: request.url, headers: request.headers });
11
- request.clikgate = { clickId };
12
+ request.clikgate = { clickId, installation: installationFromRequest(request) };
12
13
  if (clickId && persist !== false) {
13
14
  const url = new URL(request.url, 'http://localhost');
14
15
  if (url.searchParams.getAll(helpers.paramName).includes(clickId))
@@ -22,7 +23,7 @@ meta[Symbol.for('skip-override')] = true;
22
23
  meta[Symbol.for('fastify.display-name')] = '@clikgate/node/fastify';
23
24
  meta[Symbol.for('plugin-meta')] = { name: '@clikgate/node/fastify', fastify: '>=5' };
24
25
  /**
25
- * Fastify plugin: sets `request.clikgate.clickId` and keeps a click ID from the query string in the
26
+ * Fastify plugin: sets `request.clikgate.clickId` and `request.clikgate.installation`, and keeps a click ID from the query string in the
26
27
  * `_cg_<param>` cookie.
27
28
  *
28
29
  * @example