@distilled.cloud/resend 1.0.0-rc.12 → 1.0.0-rc.14

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.
@@ -1,3 +1,4 @@
1
+ import { ConfigError } from "@distilled.cloud/core/errors";
1
2
  /**
2
3
  * Resend credentials — hand-written.
3
4
  *
@@ -9,7 +10,6 @@ import * as Context from "effect/Context";
9
10
  import * as Effect from "effect/Effect";
10
11
  import * as Layer from "effect/Layer";
11
12
  import * as Redacted from "effect/Redacted";
12
- import { ConfigError } from "@distilled.cloud/core/errors";
13
13
 
14
14
  export const DEFAULT_API_BASE_URL = "https://api.resend.com";
15
15
 
@@ -18,20 +18,19 @@ export interface Config {
18
18
  readonly apiBaseUrl: string;
19
19
  }
20
20
 
21
- export class Credentials extends Context.Service<
22
- Credentials,
23
- Effect.Effect<Config>
24
- >()("ResendCredentials") {}
21
+ export class Credentials extends Context.Service<Credentials, Effect.Effect<Config>>()(
22
+ "ResendCredentials",
23
+ ) {}
25
24
 
26
- /** Layer from a plain API key + optional base URL. */
25
+ /** Layer from a redacted API key + optional base URL. */
27
26
  export const fromApiKey = (config: {
28
- readonly apiKey: string;
27
+ readonly apiKey: Redacted.Redacted<string>;
29
28
  readonly apiBaseUrl?: string;
30
29
  }): Layer.Layer<Credentials> =>
31
30
  Layer.succeed(
32
31
  Credentials,
33
32
  Effect.succeed({
34
- apiKey: Redacted.make(config.apiKey),
33
+ apiKey: config.apiKey,
35
34
  apiBaseUrl: config.apiBaseUrl ?? DEFAULT_API_BASE_URL,
36
35
  }),
37
36
  );
package/src/errors.ts CHANGED
@@ -27,8 +27,8 @@ export {
27
27
  } from "@distilled.cloud/core/errors";
28
28
  export type { DefaultErrors } from "@distilled.cloud/core/errors";
29
29
 
30
- import * as Schema from "effect/Schema";
31
30
  import * as Category from "@distilled.cloud/core/category";
31
+ import * as Schema from "effect/Schema";
32
32
 
33
33
  /** Unknown Resend error — returned when nothing else matches the failure. */
34
34
  export class UnknownResendError extends Schema.TaggedError<UnknownResendError>()(
@@ -41,10 +41,7 @@ export class UnknownResendError extends Schema.TaggedError<UnknownResendError>()
41
41
  ).pipe(Category.withServerError) {}
42
42
 
43
43
  /** Schema parse error wrapper. */
44
- export class ResendParseError extends Schema.TaggedError<ResendParseError>()(
45
- "ResendParseError",
46
- {
47
- body: Schema.Unknown,
48
- cause: Schema.Unknown,
49
- },
50
- ).pipe(Category.withParseError) {}
44
+ export class ResendParseError extends Schema.TaggedError<ResendParseError>()("ResendParseError", {
45
+ body: Schema.Unknown,
46
+ cause: Schema.Unknown,
47
+ }).pipe(Category.withParseError) {}
package/src/index.ts CHANGED
@@ -15,11 +15,7 @@
15
15
  export * from "./credentials.ts";
16
16
  export * from "./errors.ts";
17
17
  export * as T from "./traits.ts";
18
- export {
19
- ResendProtocol,
20
- type ResendOpError,
21
- type ResendOpContext,
22
- } from "./protocol.ts";
18
+ export { ResendProtocol, type ResendOpError, type ResendOpContext } from "./protocol.ts";
23
19
  export { paginateResend } from "./pagination.ts";
24
20
  export * as Retry from "./retry.ts";
25
21
  export * as Services from "./services/index.ts";
package/src/pagination.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import * as Pagination from "@distilled.cloud/core/pagination";
1
2
  /**
2
3
  * Resend pagination — hand-written.
3
4
  *
@@ -9,32 +10,23 @@
9
10
  */
10
11
  import * as Effect from "effect/Effect";
11
12
  import * as Stream from "effect/Stream";
12
- import * as Pagination from "@distilled.cloud/core/pagination";
13
13
 
14
14
  /**
15
15
  * Walk forward with `after` set to the last item's `id` while `has_more` is
16
16
  * true. An empty page or a repeated cursor terminates even if `has_more`
17
17
  * claims otherwise.
18
18
  */
19
- export const paginateResend: Pagination.PaginationStrategy = (
20
- operation,
21
- input,
22
- pagination,
23
- ) => {
19
+ export const paginateResend: Pagination.PaginationStrategy = (operation, input, pagination) => {
24
20
  const inputToken = pagination.inputToken;
25
21
  if (!inputToken) {
26
- return Stream.die(
27
- new Error("Resend cursor pagination requires inputToken"),
28
- );
22
+ return Stream.die(new Error("Resend cursor pagination requires inputToken"));
29
23
  }
30
24
  const hasNextPath = pagination.hasNextPage ?? "has_more";
31
25
  const itemsPath = pagination.items ?? "data";
32
26
 
33
27
  type State = { cursor: string | undefined; done: boolean };
34
28
  const startCursor =
35
- typeof input[inputToken] === "string"
36
- ? (input[inputToken] as string)
37
- : undefined;
29
+ typeof input[inputToken] === "string" ? (input[inputToken] as string) : undefined;
38
30
 
39
31
  return Stream.unfold({ cursor: startCursor, done: false } as State, (state) =>
40
32
  Effect.gen(function* () {
@@ -49,8 +41,7 @@ export const paginateResend: Pagination.PaginationStrategy = (
49
41
  const last = items.at(-1) as { id?: unknown } | undefined;
50
42
  const nextCursor = typeof last?.id === "string" ? last.id : undefined;
51
43
  const hasNext = Pagination.getPath(response, hasNextPath) === true;
52
- const stuckCursor =
53
- state.cursor !== undefined && nextCursor === state.cursor;
44
+ const stuckCursor = state.cursor !== undefined && nextCursor === state.cursor;
54
45
 
55
46
  return [
56
47
  response,
package/src/protocol.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import type * as API from "@distilled.cloud/core/api";
2
+ import type { API_ERRORS, ConfigError } from "@distilled.cloud/core/errors";
3
+ import { makeRestProtocol } from "@distilled.cloud/core/protocol-rest";
1
4
  /**
2
5
  * ResendProtocol — hand-written.
3
6
  *
@@ -15,15 +18,12 @@
15
18
  * shared HTTP-status classes, then {@link UnknownResendError}.
16
19
  */
17
20
  import * as Effect from "effect/Effect";
21
+ import type * as HttpClient from "effect/http/HttpClient";
22
+ import type * as HttpClientError from "effect/http/HttpClientError";
18
23
  import type * as Layer from "effect/Layer";
19
24
  import * as Redacted from "effect/Redacted";
20
- import type * as HttpClient from "effect/unstable/http/HttpClient";
21
- import type * as HttpClientError from "effect/unstable/http/HttpClientError";
22
- import type * as API from "@distilled.cloud/core/api";
23
- import { makeRestProtocol } from "@distilled.cloud/core/protocol-rest";
24
- import type { API_ERRORS, ConfigError } from "@distilled.cloud/core/errors";
25
25
  import { Credentials, type Config } from "./credentials.ts";
26
- import { UnknownResendError } from "./errors.ts";
26
+ import { UnknownResendError, ResendParseError } from "./errors.ts";
27
27
 
28
28
  /**
29
29
  * Error channel shared by every generated Resend operation. Generated service
@@ -35,48 +35,40 @@ export type ResendOpError =
35
35
  | InstanceType<(typeof API_ERRORS)[number]>
36
36
  | UnknownResendError
37
37
  | ConfigError
38
- | HttpClientError.HttpClientError;
38
+ | HttpClientError.HttpClientError
39
+ | ResendParseError;
39
40
 
40
41
  /** Context (requirements) shared by every generated Resend operation. */
41
42
  export type ResendOpContext = Credentials | HttpClient.HttpClient;
42
43
 
43
- export const ResendProtocol: Layer.Layer<API.Protocol> =
44
- makeRestProtocol<Config>({
45
- // The Credentials service holds an effect — resolving it here (per
46
- // request, on the calling fiber) picks up context-provided credentials.
47
- credentials: Effect.gen(function* () {
48
- const resolve = yield* Credentials;
49
- return yield* resolve;
50
- }),
51
- baseUrl: (creds) => creds.apiBaseUrl,
52
- headers: (creds) => ({
53
- Authorization: `Bearer ${Redacted.value(creds.apiKey)}`,
44
+ export const ResendProtocol: Layer.Layer<API.Protocol> = makeRestProtocol<Config>({
45
+ // The Credentials service holds an effect — resolving it here (per
46
+ // request, on the calling fiber) picks up context-provided credentials.
47
+ credentials: Effect.gen(function* () {
48
+ const resolve = yield* Credentials;
49
+ return yield* resolve;
50
+ }),
51
+ baseUrl: (creds) => creds.apiBaseUrl,
52
+ headers: (creds) => ({
53
+ Authorization: `Bearer ${Redacted.value(creds.apiKey)}`,
54
+ }),
55
+ // Resend's error body is `{ name?: string, message: string, statusCode?: number }`.
56
+ // The factory's default lenient envelope covers `{ code, message }`; `name`
57
+ // is the machine-readable code Resend actually sends.
58
+ errorEnvelope: (body) => {
59
+ if (body === null || typeof body !== "object") return undefined;
60
+ const rec = body as Record<string, unknown>;
61
+ const code =
62
+ typeof rec.name === "string" ? rec.name : typeof rec.code === "string" ? rec.code : undefined;
63
+ const message = typeof rec.message === "string" ? rec.message : undefined;
64
+ if (code === undefined && message === undefined) return undefined;
65
+ return { code, message };
66
+ },
67
+ unknownError: ({ code, message, body }) =>
68
+ new UnknownResendError({
69
+ code: typeof code === "string" ? code : code !== undefined ? String(code) : undefined,
70
+ message,
71
+ body,
54
72
  }),
55
- // Resend's error body is `{ name?: string, message: string, statusCode?: number }`.
56
- // The factory's default lenient envelope covers `{ code, message }`; `name`
57
- // is the machine-readable code Resend actually sends.
58
- errorEnvelope: (body) => {
59
- if (body === null || typeof body !== "object") return undefined;
60
- const rec = body as Record<string, unknown>;
61
- const code =
62
- typeof rec.name === "string"
63
- ? rec.name
64
- : typeof rec.code === "string"
65
- ? rec.code
66
- : undefined;
67
- const message = typeof rec.message === "string" ? rec.message : undefined;
68
- if (code === undefined && message === undefined) return undefined;
69
- return { code, message };
70
- },
71
- unknownError: ({ code, message, body }) =>
72
- new UnknownResendError({
73
- code:
74
- typeof code === "string"
75
- ? code
76
- : code !== undefined
77
- ? String(code)
78
- : undefined,
79
- message,
80
- body,
81
- }),
82
- });
73
+ parseError: ({ body, cause }) => new ResendParseError({ body, cause }),
74
+ });
package/src/retry.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { type Policy, throttlingFactory, transientFactory } from "@distilled.cloud/core/retry";
1
2
  /**
2
3
  * Resend retry configuration.
3
4
  *
@@ -16,11 +17,6 @@
16
17
  import * as Context from "effect/Context";
17
18
  import * as Effect from "effect/Effect";
18
19
  import * as Layer from "effect/Layer";
19
- import {
20
- type Policy,
21
- throttlingFactory,
22
- transientFactory,
23
- } from "@distilled.cloud/core/retry";
24
20
 
25
21
  export {
26
22
  type Options,
@@ -43,9 +39,7 @@ export const policy = (optionsOrFactory: Policy) =>
43
39
  Effect.provide(Layer.succeed(Retry, optionsOrFactory));
44
40
 
45
41
  /** Disables all automatic retries. */
46
- export const none = Effect.provide(
47
- Layer.succeed(Retry, { while: () => false }),
48
- );
42
+ export const none = Effect.provide(Layer.succeed(Retry, { while: () => false }));
49
43
 
50
44
  /** Apply the throttling retry policy (retries throttling errors indefinitely). */
51
45
  export const throttling = policy(throttlingFactory);