@distilled.cloud/boxd 0.0.0 → 1.0.0-rc.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/LICENSE +201 -0
- package/README.md +68 -4
- package/lib/.lazy.json +1 -0
- package/lib/_bundle/lazy.js +4 -0
- package/lib/_bundle/sdk.js +2 -0
- package/lib/_bundle/sdk.js.map +1 -0
- package/lib/credentials.d.ts +30 -0
- package/lib/credentials.d.ts.map +1 -0
- package/lib/credentials.js +1 -0
- package/lib/descriptors.d.ts +4 -0
- package/lib/descriptors.d.ts.map +1 -0
- package/lib/errors.d.ts +52 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +1 -0
- package/lib/index.d.ts +36 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +6 -0
- package/lib/index.lazy.js +6 -0
- package/lib/index.lazy.js.map +1 -0
- package/lib/protocol.d.ts +16 -0
- package/lib/protocol.d.ts.map +1 -0
- package/lib/protocol.js +1 -0
- package/lib/retry.d.ts +50 -0
- package/lib/retry.d.ts.map +1 -0
- package/lib/retry.js +1 -0
- package/lib/services/boxd.d.ts +3088 -0
- package/lib/services/boxd.d.ts.map +1 -0
- package/lib/services/boxd.js +189 -0
- package/lib/services/boxd.js.map +1 -0
- package/lib/services/index.d.ts +2 -0
- package/lib/services/index.d.ts.map +1 -0
- package/lib/services/index.js +1 -0
- package/lib/services.lazy/boxd.js +3 -0
- package/lib/services.lazy/index.js +3 -0
- package/lib/traits.d.ts +11 -0
- package/lib/traits.d.ts.map +1 -0
- package/lib/traits.js +1 -0
- package/package.json +77 -2
- package/src/credentials.ts +76 -0
- package/src/descriptors.ts +7 -0
- package/src/errors.ts +67 -0
- package/src/index.ts +35 -0
- package/src/protocol.ts +56 -0
- package/src/retry.ts +69 -0
- package/src/services/boxd.ts +9030 -0
- package/src/services/index.ts +2 -0
- package/src/traits.ts +44 -0
package/src/protocol.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ErrorOf } from "@distilled.cloud/core/protocol";
|
|
2
|
+
import { grpcProtocol } from "@distilled.cloud/core/protocol-grpc";
|
|
3
|
+
import type { RestErrorEnvelope } from "@distilled.cloud/core/protocol-rest";
|
|
4
|
+
import { unknownErrorProps } from "@distilled.cloud/core/protocol-rest";
|
|
5
|
+
/**
|
|
6
|
+
* BoxdProtocol -- gRPC-Web at boxd's method paths.
|
|
7
|
+
*
|
|
8
|
+
* Each generated operation is `POST /boxd.api.v1.BoxdApi/<Method>`. boxd's
|
|
9
|
+
* API (tonic with tonic-web) answers binary gRPC over HTTP/2 and gRPC-Web
|
|
10
|
+
* over HTTP/1.1; it answers JSON and plain `application/grpc` over HTTP/1.1
|
|
11
|
+
* with 400. So the proto3 JSON body the services build is transcoded to
|
|
12
|
+
* protobuf with the generated descriptors and sent as one
|
|
13
|
+
* `application/grpc-web+proto` frame, which plain `fetch` can send from Node,
|
|
14
|
+
* Bun, a Worker or a browser, as the official SDK's web entry does.
|
|
15
|
+
*
|
|
16
|
+
* Auth is `authorization: Bearer <API key or token>`.
|
|
17
|
+
*/
|
|
18
|
+
import * as Effect from "effect/Effect";
|
|
19
|
+
import type * as HttpClient from "effect/http/HttpClient";
|
|
20
|
+
import * as Redacted from "effect/Redacted";
|
|
21
|
+
import { Credentials } from "./credentials.ts";
|
|
22
|
+
import { descriptors } from "./descriptors.ts";
|
|
23
|
+
import { BoxdParseError, type BoxdServiceError, UnknownBoxdError } from "./errors.ts";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What every generated boxd operation fails with besides its own errors:
|
|
27
|
+
* the service's errors, the protocol's, and what resolving the credentials
|
|
28
|
+
* fails with. Generated service files annotate operations with
|
|
29
|
+
* `API.OperationMethod<I, O, BoxdOpError, BoxdOpContext>` explicitly so the
|
|
30
|
+
* compiler never infers these back out of the schema generics.
|
|
31
|
+
*/
|
|
32
|
+
export type BoxdOpError = BoxdServiceError | ErrorOf<typeof BoxdProtocol>;
|
|
33
|
+
|
|
34
|
+
/** Context (requirements) shared by every generated boxd operation. */
|
|
35
|
+
export type BoxdOpContext = Credentials | HttpClient.HttpClient;
|
|
36
|
+
|
|
37
|
+
const errorEnvelope = (body: unknown): RestErrorEnvelope | undefined => {
|
|
38
|
+
if (body === null || typeof body !== "object") return undefined;
|
|
39
|
+
const b = body as Record<string, unknown>;
|
|
40
|
+
const message = typeof b.message === "string" ? b.message : undefined;
|
|
41
|
+
const code = typeof b.code === "string" || typeof b.code === "number" ? b.code : undefined;
|
|
42
|
+
return { code, message };
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const BoxdProtocol = /*@__PURE__*/ grpcProtocol({
|
|
46
|
+
descriptors,
|
|
47
|
+
web: true,
|
|
48
|
+
// The Credentials service holds an effect -- resolving it here (per
|
|
49
|
+
// request, on the calling fiber) picks up context-provided credentials.
|
|
50
|
+
credentials: /*@__PURE__*/ Effect.flatten(Credentials),
|
|
51
|
+
baseUrl: (creds) => creds.apiBaseUrl,
|
|
52
|
+
headers: (creds) => ({ authorization: `Bearer ${Redacted.value(creds.token)}` }),
|
|
53
|
+
errorEnvelope,
|
|
54
|
+
unknownError: (info) => new UnknownBoxdError(unknownErrorProps(info)),
|
|
55
|
+
parseError: ({ body, cause }) => new BoxdParseError({ body, cause }),
|
|
56
|
+
});
|
package/src/retry.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as Retries from "@distilled.cloud/core/retry";
|
|
2
|
+
/**
|
|
3
|
+
* boxd retry surface -- a veneer over `@distilled.cloud/core/retry`.
|
|
4
|
+
*
|
|
5
|
+
* The `Retry` service tag is threaded into every generated operation via
|
|
6
|
+
* `API.make({ retry: Retry })`, so a caller-installed policy applies to all
|
|
7
|
+
* boxd calls below it and core's `makeDefault` is the fallback when none is
|
|
8
|
+
* provided.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import * as Boxd from "@distilled.cloud/boxd";
|
|
13
|
+
*
|
|
14
|
+
* myEffect.pipe(Boxd.Retry.transient);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
import * as Context from "effect/Context";
|
|
18
|
+
import * as Effect from "effect/Effect";
|
|
19
|
+
import * as Layer from "effect/Layer";
|
|
20
|
+
|
|
21
|
+
export type Options = Retries.Options;
|
|
22
|
+
export type Factory = Retries.Factory;
|
|
23
|
+
export type Policy = Retries.Policy;
|
|
24
|
+
|
|
25
|
+
/** Context tag for configuring retry behavior of boxd API calls. */
|
|
26
|
+
export class Retry extends /*@__PURE__*/ Context.Service<Retry, Policy>()("BoxdRetry") {}
|
|
27
|
+
|
|
28
|
+
/** Provides a custom retry policy to every boxd API call below it. */
|
|
29
|
+
export const policy: {
|
|
30
|
+
(
|
|
31
|
+
options: Options,
|
|
32
|
+
): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Retry>>;
|
|
33
|
+
(
|
|
34
|
+
factory: Factory,
|
|
35
|
+
): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Retry>>;
|
|
36
|
+
} = (optionsOrFactory: Options | Factory) => Effect.provide(Layer.succeed(Retry, optionsOrFactory));
|
|
37
|
+
|
|
38
|
+
/** Disables all automatic retries. */
|
|
39
|
+
export const none: <A, E, R>(
|
|
40
|
+
effect: Effect.Effect<A, E, R>,
|
|
41
|
+
) => Effect.Effect<A, E, Exclude<R, Retry>> = /*@__PURE__*/ Effect.provide(
|
|
42
|
+
/*@__PURE__*/ Layer.succeed(Retry, { while: () => false }),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The default retry policy (core's): transient/throttling/retryable errors,
|
|
47
|
+
* capped exponential backoff with jitter, server `retryAfter` hints honored
|
|
48
|
+
* with precedence.
|
|
49
|
+
*/
|
|
50
|
+
export const makeDefault: Factory = Retries.makeDefault;
|
|
51
|
+
|
|
52
|
+
export const jittered = Retries.jittered;
|
|
53
|
+
export const capped = Retries.capped;
|
|
54
|
+
|
|
55
|
+
/** Retry options that retry all throttling errors indefinitely. */
|
|
56
|
+
export const throttlingOptions: Options = Retries.throttlingOptions;
|
|
57
|
+
|
|
58
|
+
/** Retries all throttling errors indefinitely (honoring server hints). */
|
|
59
|
+
export const throttling: <A, E, R>(
|
|
60
|
+
effect: Effect.Effect<A, E, R>,
|
|
61
|
+
) => Effect.Effect<A, E, Exclude<R, Retry>> = /*@__PURE__*/ policy(Retries.throttlingFactory);
|
|
62
|
+
|
|
63
|
+
/** Retry options that retry all transient errors indefinitely. */
|
|
64
|
+
export const transientOptions: Options = Retries.transientOptions;
|
|
65
|
+
|
|
66
|
+
/** Retries all transient errors indefinitely (honoring server hints). */
|
|
67
|
+
export const transient: <A, E, R>(
|
|
68
|
+
effect: Effect.Effect<A, E, R>,
|
|
69
|
+
) => Effect.Effect<A, E, Exclude<R, Retry>> = /*@__PURE__*/ policy(Retries.transientFactory);
|