Line data Source code
1 : /**
2 : * The product side of the contract: turn a billing request into the URL to
3 : * navigate to. Failures are coded results in the style of the billing core's
4 : * `BillingResult` — the builder never throws, and a code carries no server or
5 : * caller text a host could render by accident.
6 : */
7 : import type { BillingIntent, BillingProduct } from './contract'
8 : import { billingIntentPath, isBillingSource } from './contract'
9 : import type { InvalidIdentifierCode, OptionalEntryValues } from './entryFields'
10 : import {
11 : ENTRY_PARAM_AMOUNT,
12 : ENTRY_PARAM_PRODUCT,
13 : ENTRY_PARAM_RETURN_TO,
14 : ENTRY_PARAM_SOURCE,
15 : OPTIONAL_ENTRY_FIELDS,
16 : isEntryAmountCents
17 : } from './entryFields'
18 : import { isContractIdentifier } from './identifiers'
19 : import { isReturnTarget } from './returnTargets'
20 : import { parseUrl } from './url'
21 :
22 : export interface BillingEntryInput extends OptionalEntryValues {
23 : /** Only the origin is used; any path or query on it is discarded. */
24 : readonly billingOrigin: string | URL
25 : readonly intent: BillingIntent
26 : readonly product: BillingProduct
27 : /**
28 : * A `ReturnTarget`. Typed wide because the registry lookup is the real gate:
29 : * a JavaScript host, or a generic that widened the literal, still has to
30 : * name a registered target.
31 : */
32 : readonly returnTo: string
33 : /** A `BillingSource`; anything else is left off the link. */
34 : readonly source?: string
35 : /** The credit amount a top-up asks for, in whole cents. */
36 : readonly amountCents?: number
37 : }
38 :
39 : export type BillingEntryUrlErrorCode =
40 : | 'INVALID_ORIGIN'
41 : | 'UNKNOWN_RETURN_TARGET'
42 : | 'INVALID_AMOUNT'
43 : | InvalidIdentifierCode
44 :
45 : export type BillingEntryUrlResult =
46 : | { readonly status: 'ok'; readonly url: URL }
47 : | { readonly status: 'error'; readonly code: BillingEntryUrlErrorCode }
48 :
49 2356 : const LOCAL_HOSTNAMES = new Set(['localhost', '127.0.0.1', '[::1]'])
50 :
51 0 : function billingOriginHref(origin: string | URL): string | undefined {
52 0 : const url = parseUrl(origin)
53 0 : if (!url || url.username || url.password) return undefined
54 0 : if (url.protocol === 'https:') return url.origin
55 0 : if (url.protocol === 'http:' && LOCAL_HOSTNAMES.has(url.hostname))
56 0 : return url.origin
57 : return undefined
58 : }
59 :
60 0 : function invalidIdentifierCode(
61 : values: OptionalEntryValues
62 : ): InvalidIdentifierCode | undefined {
63 0 : for (const field of OPTIONAL_ENTRY_FIELDS) {
64 0 : const value = values[field.key]
65 0 : if (value !== undefined && !isContractIdentifier(value)) return field.code
66 : }
67 : return undefined
68 : }
69 :
70 0 : export function buildBillingEntryUrl(
71 : input: BillingEntryInput
72 : ): BillingEntryUrlResult {
73 0 : const origin = billingOriginHref(input.billingOrigin)
74 0 : if (origin === undefined) return { status: 'error', code: 'INVALID_ORIGIN' }
75 :
76 0 : if (!isReturnTarget(input.returnTo))
77 0 : return { status: 'error', code: 'UNKNOWN_RETURN_TARGET' }
78 :
79 0 : const invalid = invalidIdentifierCode(input)
80 0 : if (invalid !== undefined) return { status: 'error', code: invalid }
81 :
82 0 : const { amountCents } = input
83 0 : if (amountCents !== undefined && !isEntryAmountCents(amountCents))
84 0 : return { status: 'error', code: 'INVALID_AMOUNT' }
85 :
86 0 : const url = new URL(billingIntentPath(input.intent), origin)
87 0 : url.searchParams.set(ENTRY_PARAM_PRODUCT, input.product)
88 0 : url.searchParams.set(ENTRY_PARAM_RETURN_TO, input.returnTo)
89 0 : for (const field of OPTIONAL_ENTRY_FIELDS) {
90 0 : const value = input[field.key]
91 0 : if (value !== undefined) url.searchParams.set(field.param, value)
92 : }
93 0 : if (isBillingSource(input.source))
94 0 : url.searchParams.set(ENTRY_PARAM_SOURCE, input.source)
95 0 : if (amountCents !== undefined)
96 0 : url.searchParams.set(ENTRY_PARAM_AMOUNT, String(amountCents))
97 :
98 0 : return { status: 'ok', url }
99 : }
|