@lukoweb/apitogo 0.2.46 → 0.2.49

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.
@@ -1,28 +1,28 @@
1
- {
2
- "$schema": "https://apitogo.com/schema/config-v2.json",
3
- "version": 2,
4
- "layout": "embedded",
5
- "project": { "id": "prj_123", "name": "acme-orders" },
6
- "gateway": {
7
- "origin": {
8
- "mode": "managed",
9
- "source": "server",
10
- "runtime": "nodejs",
11
- "runtimeVersion": "NODE_22_LTS"
12
- },
13
- "specs": ["server/docs/swagger.yaml"],
14
- "auth": { "mode": "identity" }
15
- },
16
- "identity": { "native": true },
17
- "plans": {
18
- "billing": {
19
- "provider": "stripe",
20
- "apiKey": "secret:STRIPE_API_KEY",
21
- "webhookSecret": "secret:STRIPE_WEBHOOK_SECRET"
22
- }
23
- },
24
- "portal": {
25
- "name": "Acme Orders",
26
- "docs": { "content": ["docs/**/*.{md,mdx}"] }
27
- }
28
- }
1
+ {
2
+ "$schema": "https://apitogo.com/schema/config-v2.json",
3
+ "version": 2,
4
+ "layout": "embedded",
5
+ "project": { "id": "prj_123", "name": "acme-orders" },
6
+ "gateway": {
7
+ "origin": {
8
+ "mode": "managed",
9
+ "source": "server",
10
+ "runtime": "nodejs",
11
+ "runtimeVersion": "NODE_22_LTS"
12
+ },
13
+ "specs": ["server/docs/swagger.yaml"],
14
+ "auth": { "mode": "identity" }
15
+ },
16
+ "identity": { "native": true },
17
+ "plans": {
18
+ "billing": {
19
+ "provider": "stripe",
20
+ "apiKey": "secret:STRIPE_API_KEY",
21
+ "webhookSecret": "secret:STRIPE_WEBHOOK_SECRET"
22
+ }
23
+ },
24
+ "portal": {
25
+ "name": "Acme Orders",
26
+ "docs": { "content": ["docs/**/*.{md,mdx}"] }
27
+ }
28
+ }
@@ -0,0 +1,59 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import type { ResolvedConfigV2 } from "./resolve.js";
3
+
4
+ /**
5
+ * Billing (decision 21, narrowed): Stripe is the only payment provider. Billing is on exactly when the plan catalog has a
6
+ * plan with a price, so a project with free and contact-us plans needs no Stripe key and is never asked for one.
7
+ */
8
+
9
+ /** The project secret that holds the Stripe key when `plans.billing.apiKey` names none. */
10
+ export const DEFAULT_STRIPE_KEY_SECRET = "STRIPE_API_KEY";
11
+
12
+ type PriceFields = { pricingModel?: unknown; priceAmount?: unknown };
13
+
14
+ /** A plan someone pays for: a fixed price above 0. A contact-us (custom) plan is not paid here. */
15
+ export function isPaidPlan(plan: PriceFields): boolean {
16
+ return (
17
+ plan.pricingModel !== "custom" &&
18
+ typeof plan.priceAmount === "number" &&
19
+ plan.priceAmount > 0
20
+ );
21
+ }
22
+
23
+ /** The skus of the paid plans in the plans file. A missing or unreadable file has none; the plans rules report it. */
24
+ export async function paidPlanSkus(
25
+ resolved: ResolvedConfigV2,
26
+ ): Promise<string[]> {
27
+ const file = resolved.plans?.file;
28
+ if (!file) return [];
29
+ try {
30
+ const raw = JSON.parse(await readFile(file, "utf8")) as {
31
+ plans?: unknown;
32
+ };
33
+ if (!Array.isArray(raw.plans)) return [];
34
+ return raw.plans
35
+ .filter(
36
+ (plan): plan is PriceFields & { sku?: unknown } =>
37
+ typeof plan === "object" && plan !== null && isPaidPlan(plan),
38
+ )
39
+ .map((plan) => String(plan.sku ?? "?"));
40
+ } catch {
41
+ return [];
42
+ }
43
+ }
44
+
45
+ /** True when the plans service takes payments: Stripe is not switched off and a plan has a price. */
46
+ export async function billingNeeded(
47
+ resolved: ResolvedConfigV2,
48
+ ): Promise<boolean> {
49
+ const plans = resolved.plans;
50
+ if (!plans?.enabled || plans.billing.provider !== "stripe") return false;
51
+ return (await paidPlanSkus(resolved)).length > 0;
52
+ }
53
+
54
+ /** The name of the secret that holds the Stripe key (`secret:NAME` in `plans.billing.apiKey`, default `STRIPE_API_KEY`). */
55
+ export function stripeKeySecretName(resolved: ResolvedConfigV2): string {
56
+ const ref = resolved.plans?.billing.apiKey;
57
+ const match = ref ? /^secret:(.+)$/.exec(ref) : null;
58
+ return match?.[1] ?? DEFAULT_STRIPE_KEY_SECRET;
59
+ }
@@ -1,72 +1,72 @@
1
- import type { InputNavigationItem } from "../validators/InputNavigationSchema.js";
2
-
3
- /** Label of the one header tab that holds the sidebar made from the docs pages. */
4
- export const DOCS_NAVIGATION_LABEL = "Documentation";
5
-
6
- type Branch = { pages: Set<string>; folders: Map<string, Branch> };
7
-
8
- const emptyBranch = (): Branch => ({ pages: new Set(), folders: new Map() });
9
-
10
- /** "getting-started" becomes "Getting started". */
11
- export function folderLabel(name: string): string {
12
- const words = name.replace(/[-_]+/g, " ").trim();
13
- return words.charAt(0).toUpperCase() + words.slice(1);
14
- }
15
-
16
- const byName = (a: string, b: string) => a.localeCompare(b);
17
-
18
- const toItems = (branch: Branch, prefix: string): InputNavigationItem[] => {
19
- const pages = [...branch.pages].sort((a, b) =>
20
- a === "index" ? -1 : b === "index" ? 1 : byName(a, b),
21
- );
22
- const folders = [...branch.folders.keys()].sort(byName);
23
- return [
24
- // The index page of a folder is the folder's own address, so the sidebar also shows on `/`.
25
- ...pages.map(
26
- (page): InputNavigationItem =>
27
- page === "index"
28
- ? {
29
- type: "doc",
30
- file: `${prefix}${page}`,
31
- path: prefix === "" ? "/" : `/${prefix.slice(0, -1)}`,
32
- }
33
- : `${prefix}${page}`,
34
- ),
35
- ...folders.map(
36
- (folder): InputNavigationItem => ({
37
- type: "category",
38
- label: folderLabel(folder),
39
- collapsible: true,
40
- collapsed: false,
41
- items: toItems(
42
- branch.folders.get(folder) ?? emptyBranch(),
43
- `${prefix}${folder}/`,
44
- ),
45
- }),
46
- ),
47
- ];
48
- };
49
-
50
- /**
51
- * The sidebar for hosted docs, made from the pages themselves: the index page first, then the other pages by
52
- * name, then one section per folder. `routes` are the docs plugin's route paths (`/overview`, `/guides/style`).
53
- * Without pages there is nothing to navigate, so the result is empty.
54
- */
55
- export function docsNavigation(routes: string[]): InputNavigationItem[] {
56
- const root = emptyBranch();
57
- for (const route of routes) {
58
- const parts = route.split("/").filter(Boolean);
59
- const page = parts.pop();
60
- if (!page) continue;
61
- const branch = parts.reduce((current, folder) => {
62
- const next = current.folders.get(folder) ?? emptyBranch();
63
- current.folders.set(folder, next);
64
- return next;
65
- }, root);
66
- branch.pages.add(page);
67
- }
68
- const items = toItems(root, "");
69
- return items.length === 0
70
- ? []
71
- : [{ type: "category", label: DOCS_NAVIGATION_LABEL, items }];
72
- }
1
+ import type { InputNavigationItem } from "../validators/InputNavigationSchema.js";
2
+
3
+ /** Label of the one header tab that holds the sidebar made from the docs pages. */
4
+ export const DOCS_NAVIGATION_LABEL = "Documentation";
5
+
6
+ type Branch = { pages: Set<string>; folders: Map<string, Branch> };
7
+
8
+ const emptyBranch = (): Branch => ({ pages: new Set(), folders: new Map() });
9
+
10
+ /** "getting-started" becomes "Getting started". */
11
+ export function folderLabel(name: string): string {
12
+ const words = name.replace(/[-_]+/g, " ").trim();
13
+ return words.charAt(0).toUpperCase() + words.slice(1);
14
+ }
15
+
16
+ const byName = (a: string, b: string) => a.localeCompare(b);
17
+
18
+ const toItems = (branch: Branch, prefix: string): InputNavigationItem[] => {
19
+ const pages = [...branch.pages].sort((a, b) =>
20
+ a === "index" ? -1 : b === "index" ? 1 : byName(a, b),
21
+ );
22
+ const folders = [...branch.folders.keys()].sort(byName);
23
+ return [
24
+ // The index page of a folder is the folder's own address, so the sidebar also shows on `/`.
25
+ ...pages.map(
26
+ (page): InputNavigationItem =>
27
+ page === "index"
28
+ ? {
29
+ type: "doc",
30
+ file: `${prefix}${page}`,
31
+ path: prefix === "" ? "/" : `/${prefix.slice(0, -1)}`,
32
+ }
33
+ : `${prefix}${page}`,
34
+ ),
35
+ ...folders.map(
36
+ (folder): InputNavigationItem => ({
37
+ type: "category",
38
+ label: folderLabel(folder),
39
+ collapsible: true,
40
+ collapsed: false,
41
+ items: toItems(
42
+ branch.folders.get(folder) ?? emptyBranch(),
43
+ `${prefix}${folder}/`,
44
+ ),
45
+ }),
46
+ ),
47
+ ];
48
+ };
49
+
50
+ /**
51
+ * The sidebar for hosted docs, made from the pages themselves: the index page first, then the other pages by
52
+ * name, then one section per folder. `routes` are the docs plugin's route paths (`/overview`, `/guides/style`).
53
+ * Without pages there is nothing to navigate, so the result is empty.
54
+ */
55
+ export function docsNavigation(routes: string[]): InputNavigationItem[] {
56
+ const root = emptyBranch();
57
+ for (const route of routes) {
58
+ const parts = route.split("/").filter(Boolean);
59
+ const page = parts.pop();
60
+ if (!page) continue;
61
+ const branch = parts.reduce((current, folder) => {
62
+ const next = current.folders.get(folder) ?? emptyBranch();
63
+ current.folders.set(folder, next);
64
+ return next;
65
+ }, root);
66
+ branch.pages.add(page);
67
+ }
68
+ const items = toItems(root, "");
69
+ return items.length === 0
70
+ ? []
71
+ : [{ type: "category", label: DOCS_NAVIGATION_LABEL, items }];
72
+ }
@@ -1,42 +1,42 @@
1
- import { ApitogoError } from "../../lib/util/invariant.js";
2
-
3
- /**
4
- * One problem in a config file. `code` is stable and safe to match on, `path` is the dotted config path
5
- * (for example `gateway.origin.mode`), `hint` says how to fix it.
6
- */
7
- export type ConfigIssue = {
8
- code: string;
9
- path: string;
10
- message: string;
11
- hint?: string;
12
- };
13
-
14
- export function formatConfigIssue(issue: ConfigIssue): string {
15
- const hint = issue.hint ? ` ${issue.hint}` : "";
16
- const rest = issue.message.slice(issue.path.length);
17
- // Several documented messages already begin with their own path; do not print it twice.
18
- const startsWithPath =
19
- issue.path !== "" &&
20
- issue.message.startsWith(issue.path) &&
21
- (rest === "" || /^[\s:]/.test(rest));
22
- const text = startsWithPath
23
- ? issue.message
24
- : `${issue.path === "" ? "(root)" : issue.path}: ${issue.message}`;
25
- return `${text}${hint} [${issue.code}]`;
26
- }
27
-
28
- /** Thrown when an `apitogo.config.json` cannot be loaded. Carries every problem found, not just the first. */
29
- export class ConfigError extends ApitogoError {
30
- public readonly issues: readonly ConfigIssue[];
31
-
32
- constructor(file: string, issues: readonly ConfigIssue[]) {
33
- super(
34
- `${file} has ${issues.length} problem${issues.length === 1 ? "" : "s"}:\n${issues
35
- .map((issue) => ` - ${formatConfigIssue(issue)}`)
36
- .join("\n")}`,
37
- { title: "Invalid apitogo config" },
38
- );
39
- this.name = "ConfigError";
40
- this.issues = issues;
41
- }
42
- }
1
+ import { ApitogoError } from "../../lib/util/invariant.js";
2
+
3
+ /**
4
+ * One problem in a config file. `code` is stable and safe to match on, `path` is the dotted config path
5
+ * (for example `gateway.origin.mode`), `hint` says how to fix it.
6
+ */
7
+ export type ConfigIssue = {
8
+ code: string;
9
+ path: string;
10
+ message: string;
11
+ hint?: string;
12
+ };
13
+
14
+ export function formatConfigIssue(issue: ConfigIssue): string {
15
+ const hint = issue.hint ? ` ${issue.hint}` : "";
16
+ const rest = issue.message.slice(issue.path.length);
17
+ // Several documented messages already begin with their own path; do not print it twice.
18
+ const startsWithPath =
19
+ issue.path !== "" &&
20
+ issue.message.startsWith(issue.path) &&
21
+ (rest === "" || /^[\s:]/.test(rest));
22
+ const text = startsWithPath
23
+ ? issue.message
24
+ : `${issue.path === "" ? "(root)" : issue.path}: ${issue.message}`;
25
+ return `${text}${hint} [${issue.code}]`;
26
+ }
27
+
28
+ /** Thrown when an `apitogo.config.json` cannot be loaded. Carries every problem found, not just the first. */
29
+ export class ConfigError extends ApitogoError {
30
+ public readonly issues: readonly ConfigIssue[];
31
+
32
+ constructor(file: string, issues: readonly ConfigIssue[]) {
33
+ super(
34
+ `${file} has ${issues.length} problem${issues.length === 1 ? "" : "s"}:\n${issues
35
+ .map((issue) => ` - ${formatConfigIssue(issue)}`)
36
+ .join("\n")}`,
37
+ { title: "Invalid apitogo config" },
38
+ );
39
+ this.name = "ConfigError";
40
+ this.issues = issues;
41
+ }
42
+ }