@otp-service/provider-email-resend 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 otp-service contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,23 @@
1
+ import { DeliveryRequest, OtpDelivery } from '@otp-service/core';
2
+
3
+ interface ResendHttpClient {
4
+ post(url: string, input: {
5
+ body: string;
6
+ headers: Record<string, string>;
7
+ }): Promise<ResendHttpResponse>;
8
+ }
9
+ interface ResendHttpResponse {
10
+ json(): Promise<unknown>;
11
+ status: number;
12
+ }
13
+ interface CreateResendEmailProviderOptions {
14
+ apiBaseUrl?: string;
15
+ apiKey: string;
16
+ from: string;
17
+ httpClient: ResendHttpClient;
18
+ subjectFormatter?: (request: DeliveryRequest) => string;
19
+ textFormatter?: (request: DeliveryRequest) => string;
20
+ }
21
+ declare function createResendEmailProvider(options: CreateResendEmailProviderOptions): OtpDelivery;
22
+
23
+ export { type CreateResendEmailProviderOptions, type ResendHttpClient, type ResendHttpResponse, createResendEmailProvider };
package/dist/index.js ADDED
@@ -0,0 +1,113 @@
1
+ import { OtpDeliveryError } from '@otp-service/core';
2
+
3
+ // src/index.ts
4
+ function createResendEmailProvider(options) {
5
+ validateOptions(options);
6
+ return {
7
+ async sendChallenge(request) {
8
+ if (request.channel !== "email") {
9
+ throw new OtpDeliveryError({
10
+ code: "UNSUPPORTED_CHANNEL",
11
+ deliveryOutcome: "DEFINITIVE_FAILURE",
12
+ message: "Resend email provider only supports email delivery.",
13
+ provider: "resend",
14
+ retryable: false
15
+ });
16
+ }
17
+ let response;
18
+ try {
19
+ response = await options.httpClient.post(buildEmailsUrl(options), {
20
+ body: JSON.stringify({
21
+ from: options.from,
22
+ subject: formatSubject(options, request),
23
+ text: formatText(options, request),
24
+ to: [request.recipient]
25
+ }),
26
+ headers: {
27
+ authorization: `Bearer ${options.apiKey}`,
28
+ "content-type": "application/json"
29
+ }
30
+ });
31
+ } catch (cause) {
32
+ throw new OtpDeliveryError({
33
+ cause,
34
+ code: "EMAIL_DELIVERY_TRANSPORT_ERROR",
35
+ deliveryOutcome: "OUTCOME_UNKNOWN",
36
+ message: "Resend email delivery transport failed before provider acceptance was confirmed.",
37
+ provider: "resend",
38
+ retryable: true
39
+ });
40
+ }
41
+ let payload;
42
+ try {
43
+ payload = await response.json();
44
+ } catch (cause) {
45
+ throw new OtpDeliveryError({
46
+ cause,
47
+ code: "INVALID_PROVIDER_RESPONSE",
48
+ deliveryOutcome: response.status >= 200 && response.status < 300 ? "OUTCOME_UNKNOWN" : "DEFINITIVE_FAILURE",
49
+ message: "Resend email provider returned an unreadable response payload.",
50
+ provider: "resend",
51
+ retryable: response.status >= 500 || response.status === 429
52
+ });
53
+ }
54
+ if (response.status < 200 || response.status >= 300) {
55
+ throw new OtpDeliveryError({
56
+ cause: payload,
57
+ code: "EMAIL_DELIVERY_FAILED",
58
+ deliveryOutcome: "DEFINITIVE_FAILURE",
59
+ message: "Resend email delivery request failed.",
60
+ provider: "resend",
61
+ retryable: response.status >= 500 || response.status === 429
62
+ });
63
+ }
64
+ assertResendSuccessResponse(payload);
65
+ }
66
+ };
67
+ }
68
+ function assertResendSuccessResponse(payload) {
69
+ if (typeof payload !== "object" || payload === null) {
70
+ throw new OtpDeliveryError({
71
+ code: "INVALID_PROVIDER_RESPONSE",
72
+ deliveryOutcome: "OUTCOME_UNKNOWN",
73
+ message: "Resend email provider returned a non-object response.",
74
+ provider: "resend",
75
+ retryable: false
76
+ });
77
+ }
78
+ const record = payload;
79
+ if (typeof record.id !== "string" || record.id.trim().length === 0) {
80
+ throw new OtpDeliveryError({
81
+ cause: payload,
82
+ code: "INVALID_PROVIDER_RESPONSE",
83
+ deliveryOutcome: "OUTCOME_UNKNOWN",
84
+ message: "Resend email provider response is missing an email ID.",
85
+ provider: "resend",
86
+ retryable: false
87
+ });
88
+ }
89
+ }
90
+ function buildEmailsUrl(options) {
91
+ const baseUrl = options.apiBaseUrl ?? "https://api.resend.com";
92
+ return `${baseUrl}/emails`;
93
+ }
94
+ function formatSubject(options, request) {
95
+ const formatter = options.subjectFormatter ?? ((deliveryRequest) => `${deliveryRequest.purpose} OTP`);
96
+ return formatter(request);
97
+ }
98
+ function formatText(options, request) {
99
+ const formatter = options.textFormatter ?? ((deliveryRequest) => `Your ${deliveryRequest.purpose} OTP is ${deliveryRequest.otp}. It expires at ${deliveryRequest.expiresAt.toISOString()}.`);
100
+ return formatter(request);
101
+ }
102
+ function validateOptions(options) {
103
+ if (options.apiKey.trim().length === 0) {
104
+ throw new Error("Resend apiKey must not be empty.");
105
+ }
106
+ if (options.from.trim().length === 0) {
107
+ throw new Error("Resend from address must not be empty.");
108
+ }
109
+ }
110
+
111
+ export { createResendEmailProvider };
112
+ //# sourceMappingURL=index.js.map
113
+ //# sourceMappingURL=index.js.map
@@ -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AAEA,EAAA,IAAI,OAAA,CAAQ,IAAA,CAAK,IAAA,EAAK,CAAE,WAAW,CAAA,EAAG;AACpC,IAAA,MAAM,IAAI,MAAM,wCAAwC,CAAA;AAAA,EAC1D;AACF","file":"index.js","sourcesContent":["import { OtpDeliveryError, type DeliveryRequest, type OtpDelivery } from \"@otp-service/core\";\n\nexport interface ResendHttpClient {\n post(\n url: string,\n input: {\n body: string;\n headers: Record<string, string>;\n }\n ): Promise<ResendHttpResponse>;\n}\n\nexport interface ResendHttpResponse {\n json(): Promise<unknown>;\n status: number;\n}\n\nexport interface CreateResendEmailProviderOptions {\n apiBaseUrl?: string;\n apiKey: string;\n from: string;\n httpClient: ResendHttpClient;\n subjectFormatter?: (request: DeliveryRequest) => string;\n textFormatter?: (request: DeliveryRequest) => string;\n}\n\ninterface ResendSuccessResponse {\n id: string;\n}\n\nexport function createResendEmailProvider(\n options: CreateResendEmailProviderOptions\n): OtpDelivery {\n validateOptions(options);\n\n return {\n async sendChallenge(request: DeliveryRequest) {\n if (request.channel !== \"email\") {\n throw new OtpDeliveryError({\n code: \"UNSUPPORTED_CHANNEL\",\n deliveryOutcome: \"DEFINITIVE_FAILURE\",\n message: \"Resend email provider only supports email delivery.\",\n provider: \"resend\",\n retryable: false\n });\n }\n\n let response: ResendHttpResponse;\n\n try {\n response = await options.httpClient.post(buildEmailsUrl(options), {\n body: JSON.stringify({\n from: options.from,\n subject: formatSubject(options, request),\n text: formatText(options, request),\n to: [request.recipient]\n }),\n headers: {\n authorization: `Bearer ${options.apiKey}`,\n \"content-type\": \"application/json\"\n }\n });\n } catch (cause) {\n throw new OtpDeliveryError({\n cause,\n code: \"EMAIL_DELIVERY_TRANSPORT_ERROR\",\n deliveryOutcome: \"OUTCOME_UNKNOWN\",\n message: \"Resend email delivery transport failed before provider acceptance was confirmed.\",\n provider: \"resend\",\n retryable: true\n });\n }\n\n let payload: unknown;\n\n try {\n payload = await response.json();\n } catch (cause) {\n throw new OtpDeliveryError({\n cause,\n code: \"INVALID_PROVIDER_RESPONSE\",\n deliveryOutcome: response.status >= 200 && response.status < 300 ? \"OUTCOME_UNKNOWN\" : \"DEFINITIVE_FAILURE\",\n message: \"Resend email provider returned an unreadable response payload.\",\n provider: \"resend\",\n retryable: response.status >= 500 || response.status === 429\n });\n }\n\n if (response.status < 200 || response.status >= 300) {\n throw new OtpDeliveryError({\n cause: payload,\n code: \"EMAIL_DELIVERY_FAILED\",\n deliveryOutcome: \"DEFINITIVE_FAILURE\",\n message: \"Resend email delivery request failed.\",\n provider: \"resend\",\n retryable: response.status >= 500 || response.status === 429\n });\n }\n\n assertResendSuccessResponse(payload);\n }\n };\n}\n\nfunction assertResendSuccessResponse(payload: unknown): asserts payload is ResendSuccessResponse {\n if (typeof payload !== \"object\" || payload === null) {\n throw new OtpDeliveryError({\n code: \"INVALID_PROVIDER_RESPONSE\",\n deliveryOutcome: \"OUTCOME_UNKNOWN\",\n message: \"Resend email provider returned a non-object response.\",\n provider: \"resend\",\n retryable: false\n });\n }\n\n const record = payload as Record<string, unknown>;\n if (typeof record.id !== \"string\" || record.id.trim().length === 0) {\n throw new OtpDeliveryError({\n cause: payload,\n code: \"INVALID_PROVIDER_RESPONSE\",\n deliveryOutcome: \"OUTCOME_UNKNOWN\",\n message: \"Resend email provider response is missing an email ID.\",\n provider: \"resend\",\n retryable: false\n });\n }\n}\n\nfunction buildEmailsUrl(options: CreateResendEmailProviderOptions): string {\n const baseUrl = options.apiBaseUrl ?? \"https://api.resend.com\";\n return `${baseUrl}/emails`;\n}\n\nfunction formatSubject(\n options: CreateResendEmailProviderOptions,\n request: DeliveryRequest\n): string {\n const formatter =\n options.subjectFormatter ??\n ((deliveryRequest: DeliveryRequest) => `${deliveryRequest.purpose} OTP`);\n\n return formatter(request);\n}\n\nfunction formatText(\n options: CreateResendEmailProviderOptions,\n request: DeliveryRequest\n): string {\n const formatter =\n options.textFormatter ??\n ((deliveryRequest: DeliveryRequest) =>\n `Your ${deliveryRequest.purpose} OTP is ${deliveryRequest.otp}. It expires at ${deliveryRequest.expiresAt.toISOString()}.`);\n\n return formatter(request);\n}\n\nfunction validateOptions(options: CreateResendEmailProviderOptions): void {\n if (options.apiKey.trim().length === 0) {\n throw new Error(\"Resend apiKey must not be empty.\");\n }\n\n if (options.from.trim().length === 0) {\n throw new Error(\"Resend from address must not be empty.\");\n }\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@otp-service/provider-email-resend",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Resend email delivery adapter for OTP challenges.",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/Suraj-H/otp-service-package-v2.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/Suraj-H/otp-service-package-v2/issues"
13
+ },
14
+ "homepage": "https://github.com/Suraj-H/otp-service-package-v2/tree/main/packages/provider-email-resend#readme",
15
+ "sideEffects": false,
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "engines": {
28
+ "node": ">=22.0.0"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "dependencies": {
34
+ "@otp-service/core": "0.1.0"
35
+ },
36
+ "scripts": {
37
+ "build": "tsup --config tsup.config.ts",
38
+ "clean": "rm -rf dist",
39
+ "lint": "eslint src test",
40
+ "test": "vitest run --config vitest.config.ts",
41
+ "typecheck": "tsc -p tsconfig.json --noEmit"
42
+ }
43
+ }