Transport, enrolment with the confirm route, token and state in payload-kv, rotation, the panel's state block, doctor rules, the fake service and the copied contract 1.5.0. Tests against the contract's runner and the fake. Docs and HTTP tests follow. Co-Authored-By: Claude Fable 5.2 <[email protected]>
63 lines
3.2 KiB
TypeScript
63 lines
3.2 KiB
TypeScript
import type { BasePayload } from 'payload'
|
|
|
|
import { type RelayOptions, resolveRelayOptions } from './options.js'
|
|
import { disconnectRelay as disconnect, enrolRelay as enrol, type EnrolResult, type RelayStatus, rotateRelayToken as rotate, getRelayStatus as status } from './session.js'
|
|
|
|
export { RELAY_SENDER_ADDRESS, relayAdapter, relayAttachmentLimit, relaySenderName } from './adapter.js'
|
|
export type { RelayAdapterArgs } from './adapter.js'
|
|
export { relayRequest } from './client.js'
|
|
export type { RelayAnswer, RelayCall, RelayRefusal } from './client.js'
|
|
export { relayConfirmEndpoint } from './confirm.js'
|
|
export {
|
|
RELAY_BASE_URL,
|
|
RELAY_CONFIRM_PATH,
|
|
RELAY_CONTRACT_VERSION,
|
|
RELAY_DEFAULTS,
|
|
RELAY_KV_KEY,
|
|
RELAY_PRODUCT,
|
|
RELAY_SERVICE_NAME,
|
|
RELAY_STATUS_CODES,
|
|
relayStatusFor,
|
|
} from './constants.js'
|
|
export type { RelayStatusCode } from './constants.js'
|
|
export { MAIL_KIND, MAIL_KINDS, mailKindOf, markNextMail, runWithMailKind, withMailKind } from './kinds.js'
|
|
export type { MailKind } from './kinds.js'
|
|
export { bareAddress, bareAddresses, buildRelayMessage, cleanSubject } from './message.js'
|
|
export type { BuildRelayMessageArgs, BuiltRelayMessage, RelaySendRequest } from './message.js'
|
|
export { isPublicHost, relayHost, resolveRelayOptions } from './options.js'
|
|
export type { RelayOptions } from './options.js'
|
|
export { sendThroughRelay } from './send.js'
|
|
export type { RelaySendContext, RelaySendResult } from './send.js'
|
|
export { setRelayToken } from './session.js'
|
|
export type { EnrolResult, RelayStatus } from './session.js'
|
|
|
|
const DISABLED = 'The mail service is switched off for this project (mail: { relay: false }).'
|
|
|
|
function resolved(payload: BasePayload, options?: RelayOptions) {
|
|
const out = resolveRelayOptions(payload, options)
|
|
if (!out) {throw new Error(DISABLED)}
|
|
return out
|
|
}
|
|
|
|
/**
|
|
* Enrols the site with the Intecion Notifications Webservice now (a deploy script, a "Connect" button
|
|
* of your own). `mailAdapter()` does this by itself on the first send. `force` also after a refusal a
|
|
* moment ago, or after "Disconnect".
|
|
*/
|
|
export const enrolRelay = (payload: BasePayload, options?: { force?: boolean } & RelayOptions): Promise<EnrolResult> => {
|
|
const { force, ...own } = options ?? {}
|
|
return enrol(payload, resolved(payload, Object.keys(own).length ? own : undefined), { force })
|
|
}
|
|
|
|
/** The site's state with the service — never the token. `refresh: true` asks the service; otherwise the stored state. */
|
|
export const getRelayStatus = (payload: BasePayload, options?: { refresh?: boolean } & RelayOptions): Promise<{ error?: string } & RelayStatus> => {
|
|
const { refresh, ...own } = options ?? {}
|
|
return status(payload, resolved(payload, Object.keys(own).length ? own : undefined), { refresh })
|
|
}
|
|
|
|
/** Gives the site's token up on the service and forgets it; the site sends nothing through the service until it is connected again. */
|
|
export const disconnectRelay = (payload: BasePayload, options?: RelayOptions): Promise<void> => disconnect(payload, resolved(payload, options))
|
|
|
|
/** Asks for a new token now (the adapter does it by itself when the service says the token is due). */
|
|
export const rotateRelayToken = (payload: BasePayload, options?: RelayOptions) => rotate(payload, resolved(payload, options), { force: true })
|