@clikgate/node 0.0.0-stage → 2.0.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.
- package/CHANGELOG.md +136 -0
- package/LICENSE +21 -0
- package/README.md +311 -2
- package/SECURITY.md +34 -0
- package/dist/cjs/ads-hashing.d.ts +37 -0
- package/dist/cjs/ads-hashing.js +67 -0
- package/dist/cjs/click-ids.d.ts +184 -0
- package/dist/cjs/click-ids.js +535 -0
- package/dist/cjs/client.d.ts +191 -0
- package/dist/cjs/client.js +291 -0
- package/dist/cjs/config.d.ts +12 -0
- package/dist/cjs/config.js +87 -0
- package/dist/cjs/core.d.ts +15 -0
- package/dist/cjs/core.js +100 -0
- package/dist/cjs/customer-id.d.ts +26 -0
- package/dist/cjs/customer-id.js +52 -0
- package/dist/cjs/enums.d.ts +48 -0
- package/dist/cjs/enums.js +47 -0
- package/dist/cjs/errors.d.ts +117 -0
- package/dist/cjs/errors.js +257 -0
- package/dist/cjs/express.d.ts +49 -0
- package/dist/cjs/express.js +44 -0
- package/dist/cjs/fastify.d.ts +47 -0
- package/dist/cjs/fastify.js +35 -0
- package/dist/cjs/generated/index.d.ts +17793 -0
- package/dist/cjs/generated/index.js +8896 -0
- package/dist/cjs/goals.d.ts +116 -0
- package/dist/cjs/goals.js +381 -0
- package/dist/cjs/guard.d.ts +17 -0
- package/dist/cjs/guard.js +40 -0
- package/dist/cjs/http.d.ts +16 -0
- package/dist/cjs/http.js +148 -0
- package/dist/cjs/identity.d.ts +42 -0
- package/dist/cjs/identity.js +133 -0
- package/dist/cjs/index.d.ts +34 -0
- package/dist/cjs/index.js +70 -0
- package/dist/cjs/known-customers.d.ts +45 -0
- package/dist/cjs/known-customers.js +95 -0
- package/dist/cjs/logger.d.ts +9 -0
- package/dist/cjs/logger.js +8 -0
- package/dist/cjs/money.d.ts +16 -0
- package/dist/cjs/money.js +51 -0
- package/dist/cjs/next.d.ts +81 -0
- package/dist/cjs/next.js +103 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/payments.d.ts +174 -0
- package/dist/cjs/payments.js +485 -0
- package/dist/cjs/resources.d.ts +109 -0
- package/dist/cjs/resources.js +189 -0
- package/dist/cjs/shutdown.d.ts +2 -0
- package/dist/cjs/shutdown.js +51 -0
- package/dist/cjs/transport.d.ts +14 -0
- package/dist/cjs/transport.js +204 -0
- package/dist/cjs/types.d.ts +218 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/user-agent.d.ts +1 -0
- package/dist/cjs/user-agent.js +53 -0
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.js +5 -0
- package/dist/cjs/webhooks.d.ts +36 -0
- package/dist/cjs/webhooks.js +108 -0
- package/dist/esm/ads-hashing.d.ts +37 -0
- package/dist/esm/ads-hashing.js +62 -0
- package/dist/esm/click-ids.d.ts +184 -0
- package/dist/esm/click-ids.js +526 -0
- package/dist/esm/client.d.ts +191 -0
- package/dist/esm/client.js +287 -0
- package/dist/esm/config.d.ts +12 -0
- package/dist/esm/config.js +82 -0
- package/dist/esm/core.d.ts +15 -0
- package/dist/esm/core.js +96 -0
- package/dist/esm/customer-id.d.ts +26 -0
- package/dist/esm/customer-id.js +47 -0
- package/dist/esm/enums.d.ts +48 -0
- package/dist/esm/enums.js +44 -0
- package/dist/esm/errors.d.ts +117 -0
- package/dist/esm/errors.js +234 -0
- package/dist/esm/express.d.ts +49 -0
- package/dist/esm/express.js +40 -0
- package/dist/esm/fastify.d.ts +47 -0
- package/dist/esm/fastify.js +32 -0
- package/dist/esm/generated/index.d.ts +17793 -0
- package/dist/esm/generated/index.js +8833 -0
- package/dist/esm/goals.d.ts +116 -0
- package/dist/esm/goals.js +371 -0
- package/dist/esm/guard.d.ts +17 -0
- package/dist/esm/guard.js +36 -0
- package/dist/esm/http.d.ts +16 -0
- package/dist/esm/http.js +138 -0
- package/dist/esm/identity.d.ts +42 -0
- package/dist/esm/identity.js +125 -0
- package/dist/esm/index.d.ts +34 -0
- package/dist/esm/index.js +23 -0
- package/dist/esm/known-customers.d.ts +45 -0
- package/dist/esm/known-customers.js +90 -0
- package/dist/esm/logger.d.ts +9 -0
- package/dist/esm/logger.js +5 -0
- package/dist/esm/money.d.ts +16 -0
- package/dist/esm/money.js +48 -0
- package/dist/esm/next.d.ts +81 -0
- package/dist/esm/next.js +96 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/payments.d.ts +174 -0
- package/dist/esm/payments.js +476 -0
- package/dist/esm/resources.d.ts +109 -0
- package/dist/esm/resources.js +183 -0
- package/dist/esm/shutdown.d.ts +2 -0
- package/dist/esm/shutdown.js +47 -0
- package/dist/esm/transport.d.ts +14 -0
- package/dist/esm/transport.js +166 -0
- package/dist/esm/types.d.ts +218 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/user-agent.d.ts +1 -0
- package/dist/esm/user-agent.js +16 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/webhooks.d.ts +36 -0
- package/dist/esm/webhooks.js +105 -0
- package/package.json +128 -4
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { clickIds as defaultClickIds } from './click-ids.js';
|
|
2
|
+
/** @internal Shared by the add-ons: read, expose and (optionally) persist the click ID. */
|
|
3
|
+
export function captureClickId(helpers, req, res, options) {
|
|
4
|
+
const clickId = helpers.fromRequest(req);
|
|
5
|
+
if (clickId && options.persist !== false) {
|
|
6
|
+
const url = new URL(req.originalUrl ?? req.url ?? '/', 'http://localhost');
|
|
7
|
+
if (url.searchParams.getAll(helpers.paramName).includes(clickId)) {
|
|
8
|
+
const { persist: _p, ...cookie } = options;
|
|
9
|
+
helpers.persist(res, clickId, cookie);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return clickId;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Express/Connect middleware: sets `req.clikgate.clickId`, and stores a click ID that arrives in
|
|
16
|
+
* the query string in the first-party `_cg_<param>` cookie. Pass the client so the organisation's
|
|
17
|
+
* click-ID parameter is used; without it, `cgid`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* app.use(clikgateMiddleware(clikgate, { days: 30 }));
|
|
21
|
+
*/
|
|
22
|
+
export function clikgateMiddleware(client, options = {}) {
|
|
23
|
+
const helpers = client ? client.clickIds : defaultClickIds;
|
|
24
|
+
let warmed = false;
|
|
25
|
+
return (req, res, next) => {
|
|
26
|
+
try {
|
|
27
|
+
if (client && !warmed) {
|
|
28
|
+
warmed = true;
|
|
29
|
+
// learn the organisation's click_id_param in the background; never blocks a request
|
|
30
|
+
void client.config().catch(() => undefined);
|
|
31
|
+
}
|
|
32
|
+
const clickId = captureClickId(helpers, req, res, options);
|
|
33
|
+
req.clikgate = { clickId };
|
|
34
|
+
next();
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
next(err);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fastify plugin: keeps the click ID (SPEC section 17). Works with Fastify 5 (Node 20+).
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* import Fastify from 'fastify';
|
|
6
|
+
* import { Clikgate } from '@clikgate/node';
|
|
7
|
+
* import { clikgatePlugin } from '@clikgate/node/fastify';
|
|
8
|
+
*
|
|
9
|
+
* const clikgate = new Clikgate();
|
|
10
|
+
* const app = Fastify();
|
|
11
|
+
* await app.register(clikgatePlugin, { client: clikgate });
|
|
12
|
+
* app.post('/signup', async (request) => {
|
|
13
|
+
* const clickId = request.clikgate.clickId;
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* @packageDocumentation
|
|
17
|
+
*/
|
|
18
|
+
import type { FastifyPluginAsync } from 'fastify';
|
|
19
|
+
import { type PersistOptions } from './click-ids.js';
|
|
20
|
+
import type { Clikgate } from './client.js';
|
|
21
|
+
/** What the plugin puts on the request. */
|
|
22
|
+
export interface ClikgateRequestContext {
|
|
23
|
+
/** The click ID, or `null`. */
|
|
24
|
+
clickId: string | null;
|
|
25
|
+
}
|
|
26
|
+
declare module 'fastify' {
|
|
27
|
+
interface FastifyRequest {
|
|
28
|
+
/** Set by `clikgatePlugin` from `@clikgate/node/fastify`. */
|
|
29
|
+
clikgate: ClikgateRequestContext;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** Plugin options. */
|
|
33
|
+
export interface ClikgatePluginOptions extends PersistOptions {
|
|
34
|
+
/** The client (for the organisation's click-ID parameter). Optional: without it, `cgid`. */
|
|
35
|
+
client?: Clikgate;
|
|
36
|
+
/** Write the cookie when the click ID arrives in the query string (default true). */
|
|
37
|
+
persist?: boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Fastify plugin: sets `request.clikgate.clickId` and keeps a click ID from the query string in the
|
|
41
|
+
* `_cg_<param>` cookie.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* await app.register(clikgatePlugin, { client: clikgate, days: 30 });
|
|
45
|
+
*/
|
|
46
|
+
export declare const clikgatePlugin: FastifyPluginAsync<ClikgatePluginOptions>;
|
|
47
|
+
export default clikgatePlugin;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { clickIds as defaultClickIds } from './click-ids.js';
|
|
2
|
+
const plugin = async (fastify, options) => {
|
|
3
|
+
const { client, persist, ...cookie } = options;
|
|
4
|
+
const helpers = client ? client.clickIds : defaultClickIds;
|
|
5
|
+
if (client)
|
|
6
|
+
void client.config().catch(() => undefined);
|
|
7
|
+
// v5: reference-type decorators must start as null and be set per request
|
|
8
|
+
fastify.decorateRequest('clikgate', null);
|
|
9
|
+
fastify.addHook('onRequest', async (request, reply) => {
|
|
10
|
+
const clickId = helpers.fromRequest({ url: request.url, headers: request.headers });
|
|
11
|
+
request.clikgate = { clickId };
|
|
12
|
+
if (clickId && persist !== false) {
|
|
13
|
+
const url = new URL(request.url, 'http://localhost');
|
|
14
|
+
if (url.searchParams.getAll(helpers.paramName).includes(clickId))
|
|
15
|
+
helpers.persist(reply, clickId, cookie);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
// What fastify-plugin does, without the dependency: share decorators and hooks with the parent scope.
|
|
20
|
+
const meta = plugin;
|
|
21
|
+
meta[Symbol.for('skip-override')] = true;
|
|
22
|
+
meta[Symbol.for('fastify.display-name')] = '@clikgate/node/fastify';
|
|
23
|
+
meta[Symbol.for('plugin-meta')] = { name: '@clikgate/node/fastify', fastify: '>=5' };
|
|
24
|
+
/**
|
|
25
|
+
* Fastify plugin: sets `request.clikgate.clickId` and keeps a click ID from the query string in the
|
|
26
|
+
* `_cg_<param>` cookie.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* await app.register(clikgatePlugin, { client: clikgate, days: 30 });
|
|
30
|
+
*/
|
|
31
|
+
export const clikgatePlugin = plugin;
|
|
32
|
+
export default clikgatePlugin;
|