@fabrika/email 0.9.0-dev.20 → 0.9.0-dev.22
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/package.json +1 -1
- package/src/http.ts +30 -4
- package/types/src/http.d.ts +4 -1
package/package.json
CHANGED
package/src/http.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// The request half both HTTP adapters share: the caller's signal, the adapter's own timeout,
|
|
2
|
-
// is
|
|
1
|
+
// The request half both HTTP adapters share: the caller's signal, the adapter's own timeout, how a thrown fetch is
|
|
2
|
+
// classified, and a redirect that is never followed. Messages name the provider and nothing the request carried.
|
|
3
3
|
|
|
4
4
|
import { EmailDeliveryError, type EmailFetch } from './email.js'
|
|
5
5
|
|
|
@@ -21,7 +21,10 @@ export interface ProviderResponse {
|
|
|
21
21
|
readonly body: unknown
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* The answer with its body already read, so an abort or a timeout while the body arrives is classified as one. A redirect
|
|
26
|
+
* is never followed: a followed one would send the credential headers and the message to wherever it points.
|
|
27
|
+
*/
|
|
25
28
|
export async function postToProvider(request: ProviderRequest): Promise<ProviderResponse> {
|
|
26
29
|
if (request.signal.aborted) throw abortedError(request.label)
|
|
27
30
|
const timeout = AbortSignal.timeout(request.timeoutMs)
|
|
@@ -32,16 +35,39 @@ export async function postToProvider(request: ProviderRequest): Promise<Provider
|
|
|
32
35
|
headers: { ...request.headers, 'Content-Type': 'application/json' },
|
|
33
36
|
signal,
|
|
34
37
|
body: JSON.stringify(request.body),
|
|
38
|
+
redirect: 'manual',
|
|
35
39
|
})
|
|
40
|
+
if (isRedirect(response)) {
|
|
41
|
+
void response.body?.cancel().catch(() => undefined)
|
|
42
|
+
throw redirectError(request.label, response.status)
|
|
43
|
+
}
|
|
36
44
|
const text = await untilAborted(response.text(), signal)
|
|
37
45
|
return { ok: response.ok, status: response.status, body: parseJson(text) }
|
|
38
|
-
} catch {
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if (error instanceof EmailDeliveryError) throw error
|
|
39
48
|
if (request.signal.aborted) throw abortedError(request.label)
|
|
40
49
|
if (timeout.aborted) throw new EmailDeliveryError(`${request.label} request timed out`, { code: 'request_timeout' })
|
|
41
50
|
throw new EmailDeliveryError(`${request.label} request failed`, { code: 'network_error' })
|
|
42
51
|
}
|
|
43
52
|
}
|
|
44
53
|
|
|
54
|
+
/**
|
|
55
|
+
* A 3xx, or the opaque answer a browser-like runtime gives for one under `redirect: 'manual'`. The endpoint answered
|
|
56
|
+
* without taking the message, and the same request gets the same answer, so it is a permanent `provider_rejected`: the
|
|
57
|
+
* configured URL is not the provider's API, and a person fixes it.
|
|
58
|
+
*/
|
|
59
|
+
function isRedirect(response: Response): boolean {
|
|
60
|
+
return response.type === 'opaqueredirect' || (response.status >= 300 && response.status < 400)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function redirectError(label: string, status: number): EmailDeliveryError {
|
|
64
|
+
const shown = status === 0 ? '' : ` (HTTP ${status})`
|
|
65
|
+
return new EmailDeliveryError(`${label} answered with a redirect${shown}, which is not followed`, {
|
|
66
|
+
code: 'provider_rejected',
|
|
67
|
+
...(status === 0 ? {} : { httpStatus: status }),
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
45
71
|
export function parseTimeout(label: string, value: number | undefined): number {
|
|
46
72
|
const timeoutMs = value ?? 10_000
|
|
47
73
|
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 120_000) {
|
package/types/src/http.d.ts
CHANGED
|
@@ -15,6 +15,9 @@ export interface ProviderResponse {
|
|
|
15
15
|
/** The parsed JSON body, or undefined when it is not JSON. Never put into an error. */
|
|
16
16
|
readonly body: unknown;
|
|
17
17
|
}
|
|
18
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* The answer with its body already read, so an abort or a timeout while the body arrives is classified as one. A redirect
|
|
20
|
+
* is never followed: a followed one would send the credential headers and the message to wherever it points.
|
|
21
|
+
*/
|
|
19
22
|
export declare function postToProvider(request: ProviderRequest): Promise<ProviderResponse>;
|
|
20
23
|
export declare function parseTimeout(label: string, value: number | undefined): number;
|