@9d8/pager 0.1.0-beta.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/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # pager
2
+
3
+ Minimal SDK for sending pages to a Pager deployment.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ bun add pager
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { page } from "pager";
15
+
16
+ await page("Database latency is climbing", ["prod", "db"]);
17
+ ```
18
+
19
+ ## Environment
20
+
21
+ - `PAGER_API_KEY`: required API key created in the Pager dashboard
22
+ - `PAGER_API_URL`: optional base URL override, useful for local or staging deployments
23
+
24
+ Example:
25
+
26
+ ```bash
27
+ export PAGER_API_KEY=pager_...
28
+ export PAGER_API_URL=http://localhost:3000/api/v1
29
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createPagerClient: () => createPagerClient,
24
+ page: () => page
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_fetch = require("@better-fetch/fetch");
28
+ var DEFAULT_PAGER_API_URL = "https://pager.dev/api/v1";
29
+ var pageImpl = null;
30
+ var cachedApiKey;
31
+ var cachedApiUrl;
32
+ function createPagerClient(options = {}) {
33
+ const apiKey = options.apiKey ?? process.env.PAGER_API_KEY;
34
+ const baseURL = options.baseURL ?? process.env.PAGER_API_URL ?? DEFAULT_PAGER_API_URL;
35
+ const fetchFactory = options.fetchFactory ?? import_fetch.createFetch;
36
+ if (!apiKey) {
37
+ throw new Error("PAGER_API_KEY is not set");
38
+ }
39
+ const $fetch = fetchFactory({
40
+ baseURL,
41
+ throw: true,
42
+ headers: {
43
+ "X-API-Key": apiKey
44
+ }
45
+ });
46
+ return async (message, tags) => {
47
+ const data = await $fetch("/", {
48
+ method: "POST",
49
+ body: {
50
+ message,
51
+ tags: tags?.length ? tags : void 0
52
+ }
53
+ });
54
+ return data.messageId;
55
+ };
56
+ }
57
+ function getPageImpl() {
58
+ const apiKey = process.env.PAGER_API_KEY;
59
+ const apiUrl = process.env.PAGER_API_URL ?? DEFAULT_PAGER_API_URL;
60
+ if (pageImpl && cachedApiKey === apiKey && cachedApiUrl === apiUrl) {
61
+ return pageImpl;
62
+ }
63
+ cachedApiKey = apiKey;
64
+ cachedApiUrl = apiUrl;
65
+ pageImpl = createPagerClient({
66
+ apiKey,
67
+ baseURL: apiUrl
68
+ });
69
+ return pageImpl;
70
+ }
71
+ async function page(message, tags) {
72
+ return getPageImpl()(message, tags);
73
+ }
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ createPagerClient,
77
+ page
78
+ });
@@ -0,0 +1,23 @@
1
+ import { createFetch } from '@better-fetch/fetch';
2
+
3
+ type PageFn = (message: string, tags?: string[]) => Promise<string>;
4
+ type FetchFactory = typeof createFetch;
5
+ type PagerClientOptions = {
6
+ apiKey?: string;
7
+ baseURL?: string;
8
+ fetchFactory?: FetchFactory;
9
+ };
10
+ declare function createPagerClient(options?: PagerClientOptions): PageFn;
11
+ /**
12
+ * Page a message to the pager, pass in the message and optional tags
13
+ *
14
+ * Pager API Key can be created in the pager dashboard
15
+ * https://pager.dev/settings
16
+ *
17
+ * @param message - The message to page
18
+ * @param tags - Optional tags to add to the message
19
+ * @returns The messageId from the pager API
20
+ */
21
+ declare function page(message: string, tags?: string[]): Promise<string>;
22
+
23
+ export { createPagerClient, page };
@@ -0,0 +1,23 @@
1
+ import { createFetch } from '@better-fetch/fetch';
2
+
3
+ type PageFn = (message: string, tags?: string[]) => Promise<string>;
4
+ type FetchFactory = typeof createFetch;
5
+ type PagerClientOptions = {
6
+ apiKey?: string;
7
+ baseURL?: string;
8
+ fetchFactory?: FetchFactory;
9
+ };
10
+ declare function createPagerClient(options?: PagerClientOptions): PageFn;
11
+ /**
12
+ * Page a message to the pager, pass in the message and optional tags
13
+ *
14
+ * Pager API Key can be created in the pager dashboard
15
+ * https://pager.dev/settings
16
+ *
17
+ * @param message - The message to page
18
+ * @param tags - Optional tags to add to the message
19
+ * @returns The messageId from the pager API
20
+ */
21
+ declare function page(message: string, tags?: string[]): Promise<string>;
22
+
23
+ export { createPagerClient, page };
package/dist/index.js ADDED
@@ -0,0 +1,52 @@
1
+ // index.ts
2
+ import { createFetch } from "@better-fetch/fetch";
3
+ var DEFAULT_PAGER_API_URL = "https://pager.dev/api/v1";
4
+ var pageImpl = null;
5
+ var cachedApiKey;
6
+ var cachedApiUrl;
7
+ function createPagerClient(options = {}) {
8
+ const apiKey = options.apiKey ?? process.env.PAGER_API_KEY;
9
+ const baseURL = options.baseURL ?? process.env.PAGER_API_URL ?? DEFAULT_PAGER_API_URL;
10
+ const fetchFactory = options.fetchFactory ?? createFetch;
11
+ if (!apiKey) {
12
+ throw new Error("PAGER_API_KEY is not set");
13
+ }
14
+ const $fetch = fetchFactory({
15
+ baseURL,
16
+ throw: true,
17
+ headers: {
18
+ "X-API-Key": apiKey
19
+ }
20
+ });
21
+ return async (message, tags) => {
22
+ const data = await $fetch("/", {
23
+ method: "POST",
24
+ body: {
25
+ message,
26
+ tags: tags?.length ? tags : void 0
27
+ }
28
+ });
29
+ return data.messageId;
30
+ };
31
+ }
32
+ function getPageImpl() {
33
+ const apiKey = process.env.PAGER_API_KEY;
34
+ const apiUrl = process.env.PAGER_API_URL ?? DEFAULT_PAGER_API_URL;
35
+ if (pageImpl && cachedApiKey === apiKey && cachedApiUrl === apiUrl) {
36
+ return pageImpl;
37
+ }
38
+ cachedApiKey = apiKey;
39
+ cachedApiUrl = apiUrl;
40
+ pageImpl = createPagerClient({
41
+ apiKey,
42
+ baseURL: apiUrl
43
+ });
44
+ return pageImpl;
45
+ }
46
+ async function page(message, tags) {
47
+ return getPageImpl()(message, tags);
48
+ }
49
+ export {
50
+ createPagerClient,
51
+ page
52
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@9d8/pager",
3
+ "version": "0.1.0-beta.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./dist/index.d.ts",
8
+ "import": "./dist/index.js",
9
+ "require": "./dist/index.cjs"
10
+ }
11
+ },
12
+ "main": "./dist/index.cjs",
13
+ "module": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup",
20
+ "test": "bun test"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "devDependencies": {
26
+ "@types/bun": "latest",
27
+ "tsup": "^8.5.1"
28
+ },
29
+ "peerDependencies": {
30
+ "typescript": "^5"
31
+ },
32
+ "dependencies": {
33
+ "@better-fetch/fetch": "^1.1.21"
34
+ }
35
+ }