Line data Source code
1 : /**
2 : * The query shape of an entry URL, in one table so the builder and the parser
3 : * cannot drift apart: a field is written and read under the same name, and a
4 : * value that fails the shared charset reports the same code on both sides.
5 : * The one exception is `promo`: the parser carries an unreadable one apart
6 : * rather than refusing the link, because it only prefills a field.
7 : */
8 :
9 : /** Names are provisional; see `contract.ts`. */
10 2356 : export const ENTRY_PARAM_PRODUCT = 'product'
11 2356 : export const ENTRY_PARAM_RETURN_TO = 'return_to'
12 : /** Also what a return URL names the billed workspace under. */
13 2356 : export const ENTRY_PARAM_WORKSPACE = 'workspace'
14 : /** Outside the table below: a source off the shared list is dropped, never an error. */
15 2356 : export const ENTRY_PARAM_SOURCE = 'source'
16 :
17 : export type OptionalEntryKey =
18 : | 'plan'
19 : | 'correlationId'
20 : | 'workspaceId'
21 : | 'teamCreditStopId'
22 : | 'promotionCode'
23 :
24 : export type InvalidIdentifierCode =
25 : | 'INVALID_PLAN'
26 : | 'INVALID_CORRELATION_ID'
27 : | 'INVALID_WORKSPACE_ID'
28 : | 'INVALID_TEAM_CREDIT_STOP_ID'
29 : | 'INVALID_PROMOTION_CODE'
30 :
31 : interface OptionalEntryField {
32 : readonly key: OptionalEntryKey
33 : readonly param: string
34 : readonly code: InvalidIdentifierCode
35 : }
36 :
37 2356 : export const OPTIONAL_ENTRY_FIELDS: readonly OptionalEntryField[] = [
38 : { key: 'plan', param: 'plan', code: 'INVALID_PLAN' },
39 : {
40 : key: 'correlationId',
41 : param: 'correlation_id',
42 : code: 'INVALID_CORRELATION_ID'
43 : },
44 : {
45 : key: 'workspaceId',
46 : param: ENTRY_PARAM_WORKSPACE,
47 : code: 'INVALID_WORKSPACE_ID'
48 : },
49 : {
50 : key: 'teamCreditStopId',
51 : param: 'team_credit_stop_id',
52 : code: 'INVALID_TEAM_CREDIT_STOP_ID'
53 : },
54 : /** Prefills checkout's promo field; billing applies it only on the customer's Apply. */
55 : { key: 'promotionCode', param: 'promo', code: 'INVALID_PROMOTION_CODE' }
56 : ]
57 :
58 : export type OptionalEntryValues = {
59 : readonly [K in OptionalEntryKey]?: string
60 : }
61 :
62 : /**
63 : * The credit amount a top-up link asks for, in whole cents. It is the
64 : * customer's chosen quantity, not a price: billing quotes the credits it
65 : * buys from the server.
66 : */
67 2356 : export const ENTRY_PARAM_AMOUNT = 'amount_cents'
68 :
69 : /** At most $9,999,999.99; the server applies its own limits on top. */
70 2356 : const MAX_AMOUNT_CENTS = 999_999_999
71 :
72 : const WHOLE_CENTS = /^[1-9][0-9]{0,8}$/
73 :
74 0 : export function isEntryAmountCents(value: number): boolean {
75 0 : return Number.isInteger(value) && value > 0 && value <= MAX_AMOUNT_CENTS
76 : }
77 :
78 : export function readEntryAmountCents(raw: string): number | undefined {
79 : return WHOLE_CENTS.test(raw) ? Number(raw) : undefined
80 : }
|