@distilled.cloud/mux 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 +122 -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 +66 -0
- package/lib/credentials.d.ts.map +1 -0
- package/lib/credentials.js +1 -0
- package/lib/errors.d.ts +45 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +1 -0
- package/lib/index.d.ts +44 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +6 -0
- package/lib/pagination.d.ts +11 -0
- package/lib/pagination.d.ts.map +1 -0
- package/lib/pagination.js +1 -0
- package/lib/protocol.d.ts +19 -0
- package/lib/protocol.d.ts.map +1 -0
- package/lib/protocol.js +1 -0
- package/lib/retry.d.ts +54 -0
- package/lib/retry.d.ts.map +1 -0
- package/lib/retry.js +1 -0
- package/lib/services/data.d.ts +1347 -0
- package/lib/services/data.d.ts.map +1 -0
- package/lib/services/data.js +58 -0
- package/lib/services/data.js.map +1 -0
- package/lib/services/index.d.ts +5 -0
- package/lib/services/index.d.ts.map +1 -0
- package/lib/services/index.js +4 -0
- package/lib/services/robots.d.ts +2206 -0
- package/lib/services/robots.d.ts.map +1 -0
- package/lib/services/robots.js +75 -0
- package/lib/services/robots.js.map +1 -0
- package/lib/services/system.d.ts +263 -0
- package/lib/services/system.d.ts.map +1 -0
- package/lib/services/system.js +2 -0
- package/lib/services/system.js.map +1 -0
- package/lib/services/video.d.ts +1564 -0
- package/lib/services/video.d.ts.map +1 -0
- package/lib/services/video.js +100 -0
- package/lib/services/video.js.map +1 -0
- package/lib/services.lazy/data.js +3 -0
- package/lib/services.lazy/index.js +3 -0
- package/lib/services.lazy/robots.js +4 -0
- package/lib/services.lazy/system.js +1 -0
- package/lib/services.lazy/video.js +4 -0
- package/lib/traits.d.ts +7 -0
- package/lib/traits.d.ts.map +1 -0
- package/lib/traits.js +1 -0
- package/package.json +82 -2
- package/src/credentials.ts +142 -0
- package/src/errors.ts +59 -0
- package/src/index.ts +43 -0
- package/src/pagination.ts +63 -0
- package/src/protocol.ts +62 -0
- package/src/retry.ts +73 -0
- package/src/services/data.ts +3993 -0
- package/src/services/index.ts +5 -0
- package/src/services/robots.ts +4712 -0
- package/src/services/system.ts +752 -0
- package/src/services/video.ts +4291 -0
- package/src/traits.ts +40 -0
package/src/errors.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mux error types.
|
|
3
|
+
*
|
|
4
|
+
* Failures arrive as `{ "error": { "type": "not_found", "messages": [...] } }`
|
|
5
|
+
* and map by status onto the core classes re-exported here: `NotFound`,
|
|
6
|
+
* `BadRequest`, `Unauthorized`, `Forbidden`, `TooManyRequests` and the 5xx
|
|
7
|
+
* family, the last two retryable. Operations that declare a status get a
|
|
8
|
+
* generated class with the same `_tag` (and `PreconditionFailed` on
|
|
9
|
+
* `listUsageExports`), so `catchTag` works against either.
|
|
10
|
+
*/
|
|
11
|
+
export {
|
|
12
|
+
BadGateway,
|
|
13
|
+
BadRequest,
|
|
14
|
+
Conflict,
|
|
15
|
+
ConfigError,
|
|
16
|
+
Forbidden,
|
|
17
|
+
GatewayTimeout,
|
|
18
|
+
InternalServerError,
|
|
19
|
+
Locked,
|
|
20
|
+
NotFound,
|
|
21
|
+
ServiceUnavailable,
|
|
22
|
+
TooManyRequests,
|
|
23
|
+
Unauthorized,
|
|
24
|
+
UnprocessableEntity,
|
|
25
|
+
HTTP_STATUS_MAP,
|
|
26
|
+
DEFAULT_ERRORS,
|
|
27
|
+
API_ERRORS,
|
|
28
|
+
} from "@distilled.cloud/core/errors";
|
|
29
|
+
export type { DefaultErrors } from "@distilled.cloud/core/errors";
|
|
30
|
+
|
|
31
|
+
import * as Category from "@distilled.cloud/core/category";
|
|
32
|
+
import { API_ERRORS, UnknownErrorFields } from "@distilled.cloud/core/errors";
|
|
33
|
+
import * as Schema from "@distilled.cloud/core/schema-lite-typed";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A non-2xx, non-5xx status that neither the operation nor the core map
|
|
37
|
+
* covers. `code` is Mux's `error.type` (`invalid_parameters`,
|
|
38
|
+
* `precondition_failed`, ...) when the body carries one.
|
|
39
|
+
*/
|
|
40
|
+
export class UnknownMuxError
|
|
41
|
+
extends /*@__PURE__*/ Schema.TaggedError<UnknownMuxError>()(
|
|
42
|
+
"UnknownMuxError",
|
|
43
|
+
UnknownErrorFields,
|
|
44
|
+
) {}
|
|
45
|
+
|
|
46
|
+
/** A 2xx response that does not match the operation's output schema. */
|
|
47
|
+
export class MuxParseError
|
|
48
|
+
extends /*@__PURE__*/ Schema.TaggedError<MuxParseError>()("MuxParseError", {
|
|
49
|
+
body: Schema.Unknown,
|
|
50
|
+
cause: Schema.Unknown,
|
|
51
|
+
}).pipe(Category.withParseError) {}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The errors declared on the Mux service: any operation can return them,
|
|
55
|
+
* and every generated operation lists them as its `serviceErrors`. Today that
|
|
56
|
+
* is each of core's status classes (401, 403, 404, 429, the 5xx ...).
|
|
57
|
+
*/
|
|
58
|
+
export const MuxServiceErrors = API_ERRORS;
|
|
59
|
+
export type MuxServiceError = InstanceType<(typeof MuxServiceErrors)[number]>;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @distilled.cloud/mux -- Mux API SDK for Effect.
|
|
3
|
+
*
|
|
4
|
+
* `./services` is generated by `scripts/generate.ts` from the Smithy models
|
|
5
|
+
* that `scripts/convert.ts` converts from Mux's OpenAPI document
|
|
6
|
+
* (https://www.mux.com/api-spec.json), mirrored at
|
|
7
|
+
* `specs/spec-mirror-mux/specs/api-spec.json`. Everything else in this folder
|
|
8
|
+
* is hand-written.
|
|
9
|
+
*
|
|
10
|
+
* One module per API group, imported from its subpath: `video` (assets, live
|
|
11
|
+
* streams, uploads, playback), `data` (views, metrics, monitoring),
|
|
12
|
+
* `system` (signing keys, webhooks, usage) and `robots` (AI jobs).
|
|
13
|
+
*
|
|
14
|
+
* The image.mux.com and stream.mux.com playback URLs (thumbnails, HLS) are
|
|
15
|
+
* not API calls and are not modeled.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import * as Effect from "effect/Effect";
|
|
20
|
+
* import * as Layer from "effect/Layer";
|
|
21
|
+
* import * as FetchHttpClient from "effect/http/FetchHttpClient";
|
|
22
|
+
* import * as Mux from "@distilled.cloud/mux";
|
|
23
|
+
* import * as System from "@distilled.cloud/mux/system";
|
|
24
|
+
* import * as Video from "@distilled.cloud/mux/video";
|
|
25
|
+
*
|
|
26
|
+
* const program = Effect.gen(function* () {
|
|
27
|
+
* const { data: whoami } = yield* System.getWhoami({});
|
|
28
|
+
* const { data: assets } = yield* Video.listAssets({ limit: 5 });
|
|
29
|
+
* return { environment: whoami.environment_name, assets: assets.map((a) => a.id) };
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* program.pipe(
|
|
33
|
+
* Effect.provide(Layer.mergeAll(FetchHttpClient.layer, Mux.CredentialsFromEnv)),
|
|
34
|
+
* Effect.runPromise,
|
|
35
|
+
* );
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export * from "./credentials.ts";
|
|
39
|
+
export * from "./errors.ts";
|
|
40
|
+
export * as T from "./traits.ts";
|
|
41
|
+
export { MuxProtocol, type MuxOpError, type MuxOpContext } from "./protocol.ts";
|
|
42
|
+
export { paginatePage } from "./pagination.ts";
|
|
43
|
+
export * as Retry from "./retry.ts";
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getPath,
|
|
3
|
+
missingPaginationConfig,
|
|
4
|
+
type PaginationStrategy,
|
|
5
|
+
} from "@distilled.cloud/core/pagination";
|
|
6
|
+
/**
|
|
7
|
+
* Mux pagination -- hand-written.
|
|
8
|
+
*
|
|
9
|
+
* Mux lists take `page` (1-based) and `limit` and answer `{ data: [...] }`.
|
|
10
|
+
* Nothing in the response names the next page, and most lists report no
|
|
11
|
+
* total, so core's `paginatePageNumber` (which reads the next page from the
|
|
12
|
+
* response) cannot drive them. This strategy does what Mux's official SDKs
|
|
13
|
+
* do (`muxinc/mux-ts` `src/core/pagination.ts`: `BasePage` asks for
|
|
14
|
+
* `page + 1` and stops once a page has no items): request `page`, `page+1`,
|
|
15
|
+
* ... until a page comes back empty.
|
|
16
|
+
*/
|
|
17
|
+
import * as Effect from "effect/Effect";
|
|
18
|
+
import * as Stream from "effect/Stream";
|
|
19
|
+
|
|
20
|
+
export { getItems } from "@distilled.cloud/core/pagination";
|
|
21
|
+
|
|
22
|
+
/** A copy of `obj` with the member at `path` replaced by `value`. */
|
|
23
|
+
const setPath = (obj: unknown, [head, ...rest]: ReadonlyArray<string>, value: unknown): unknown => {
|
|
24
|
+
const record = typeof obj === "object" && obj !== null ? (obj as Record<string, unknown>) : {};
|
|
25
|
+
return { ...record, [head!]: rest.length === 0 ? value : setPath(record[head!], rest, value) };
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Stream of pages: `page`, `page+1`, ... until a page has no items.
|
|
30
|
+
*
|
|
31
|
+
* A page whose items member is present but not an array has no items. Mux
|
|
32
|
+
* sends `{ "data": {} }` for an empty `listDrmConfigurations`; that page is
|
|
33
|
+
* emitted with `data: []` so `.items()` never yields the `{}` as an item.
|
|
34
|
+
*/
|
|
35
|
+
export const paginatePage: PaginationStrategy = (operation, input, pagination) => {
|
|
36
|
+
const inputToken = pagination.inputToken;
|
|
37
|
+
const itemsPath = pagination.items;
|
|
38
|
+
if (!inputToken || !itemsPath) {
|
|
39
|
+
return missingPaginationConfig("Mux page pagination requires inputToken and items");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type State = { page: number; done: boolean };
|
|
43
|
+
const start = typeof input[inputToken] === "number" ? (input[inputToken] as number) : 1;
|
|
44
|
+
|
|
45
|
+
return Stream.unfold({ page: start, done: false } as State, (state) =>
|
|
46
|
+
Effect.gen(function* () {
|
|
47
|
+
if (state.done) return undefined;
|
|
48
|
+
|
|
49
|
+
const response = yield* operation({ ...input, [inputToken]: state.page } as typeof input);
|
|
50
|
+
const items = getPath(response, itemsPath);
|
|
51
|
+
const isList = Array.isArray(items);
|
|
52
|
+
const page =
|
|
53
|
+
isList || items == null
|
|
54
|
+
? response
|
|
55
|
+
: (setPath(response, itemsPath.split("."), []) as typeof response);
|
|
56
|
+
|
|
57
|
+
return [
|
|
58
|
+
page,
|
|
59
|
+
{ page: state.page + 1, done: !isList || items.length === 0 } satisfies State,
|
|
60
|
+
] as const;
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
};
|
package/src/protocol.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ErrorOf } from "@distilled.cloud/core/protocol";
|
|
2
|
+
import { restProtocol, unknownErrorProps } from "@distilled.cloud/core/protocol-rest";
|
|
3
|
+
/**
|
|
4
|
+
* MuxProtocol -- hand-written.
|
|
5
|
+
*
|
|
6
|
+
* Plain JSON REST against https://api.mux.com. Responses are returned as the
|
|
7
|
+
* wire sends them -- Mux wraps resources as `{ "data": ... }` and the SDK
|
|
8
|
+
* keeps that shape, so `getAsset` answers `{ data: Asset }`. Failures are
|
|
9
|
+
*
|
|
10
|
+
* { "error": { "type": "not_found", "messages": ["Asset not found"] } }
|
|
11
|
+
*
|
|
12
|
+
* mapped by status;
|
|
13
|
+
* `type` becomes the error's `code` and the messages its `message`.
|
|
14
|
+
*/
|
|
15
|
+
import * as Effect from "effect/Effect";
|
|
16
|
+
import type * as HttpClient from "effect/http/HttpClient";
|
|
17
|
+
import { authorizationHeader, Credentials } from "./credentials.ts";
|
|
18
|
+
import { MuxParseError, UnknownMuxError, type MuxServiceError } from "./errors.ts";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* What every generated Mux operation fails with besides its own errors: the
|
|
22
|
+
* errors declared on the service, and the errors declared on the protocol.
|
|
23
|
+
* Nobody lists the protocol's: its type carries what its code fails with (a
|
|
24
|
+
* response that does not match its schema, a failed response nothing declares,
|
|
25
|
+
* core's REST errors, and what resolving the credentials fails with).
|
|
26
|
+
* Generated service files annotate operations with `API.OperationMethod<I, O,
|
|
27
|
+
* MuxOpError, MuxOpContext>` explicitly so the compiler never infers these
|
|
28
|
+
* back out of the schema generics.
|
|
29
|
+
*/
|
|
30
|
+
export type MuxOpError = MuxServiceError | ErrorOf<typeof MuxProtocol>;
|
|
31
|
+
|
|
32
|
+
/** Context (requirements) shared by every generated Mux operation. */
|
|
33
|
+
export type MuxOpContext = Credentials | HttpClient.HttpClient;
|
|
34
|
+
|
|
35
|
+
const isRecord = (v: unknown): v is Record<string, unknown> => typeof v === "object" && v !== null;
|
|
36
|
+
|
|
37
|
+
const str = (v: unknown): string | undefined => (typeof v === "string" && v !== "" ? v : undefined);
|
|
38
|
+
|
|
39
|
+
/** `{ error: { type, messages, message? } }` -> `{ code, message }`. */
|
|
40
|
+
const muxError = (body: unknown): { code?: string; message?: string } | undefined => {
|
|
41
|
+
const error = isRecord(body) && isRecord(body.error) ? body.error : undefined;
|
|
42
|
+
if (error === undefined) return undefined;
|
|
43
|
+
const messages = Array.isArray(error.messages)
|
|
44
|
+
? error.messages.filter((m): m is string => typeof m === "string" && m !== "")
|
|
45
|
+
: [];
|
|
46
|
+
const message = messages.length > 0 ? messages.join("; ") : str(error.message);
|
|
47
|
+
const code = str(error.type);
|
|
48
|
+
return code === undefined && message === undefined ? undefined : { code, message };
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const MuxProtocol = /*@__PURE__*/ restProtocol({
|
|
52
|
+
// Resolved on the calling fiber per request.
|
|
53
|
+
credentials: /*@__PURE__*/ Effect.flatten(Credentials),
|
|
54
|
+
baseUrl: (creds) => creds.apiBaseUrl,
|
|
55
|
+
headers: (creds) => ({
|
|
56
|
+
Authorization: authorizationHeader(creds.auth),
|
|
57
|
+
Accept: "application/json",
|
|
58
|
+
}),
|
|
59
|
+
errorEnvelope: muxError,
|
|
60
|
+
unknownError: (info) => new UnknownMuxError(unknownErrorProps(info)),
|
|
61
|
+
parseError: ({ body, cause }) => new MuxParseError({ body, cause }),
|
|
62
|
+
});
|
package/src/retry.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import * as Retries from "@distilled.cloud/core/retry";
|
|
2
|
+
/**
|
|
3
|
+
* Mux retry surface -- a veneer over `@distilled.cloud/core/retry`.
|
|
4
|
+
*
|
|
5
|
+
* Mux rate-limits per environment and answers 429 past the limit (the
|
|
6
|
+
* System API documents it as `RateLimitedResponse`); `throttling` is the
|
|
7
|
+
* policy that recovers it.
|
|
8
|
+
*
|
|
9
|
+
* The `Retry` service tag is threaded into every generated operation via
|
|
10
|
+
* `API.make({ retry: Retry })`, so a caller-installed policy applies to all
|
|
11
|
+
* Mux calls below it and core's `makeDefault` is the fallback when none is
|
|
12
|
+
* provided.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import * as Mux from "@distilled.cloud/mux";
|
|
17
|
+
*
|
|
18
|
+
* myEffect.pipe(Mux.Retry.throttling);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import * as Context from "effect/Context";
|
|
22
|
+
import * as Effect from "effect/Effect";
|
|
23
|
+
import * as Layer from "effect/Layer";
|
|
24
|
+
|
|
25
|
+
export type Options = Retries.Options;
|
|
26
|
+
export type Factory = Retries.Factory;
|
|
27
|
+
export type Policy = Retries.Policy;
|
|
28
|
+
|
|
29
|
+
/** Context tag for configuring retry behavior of Mux API calls. */
|
|
30
|
+
export class Retry extends /*@__PURE__*/ Context.Service<Retry, Policy>()("MuxRetry") {}
|
|
31
|
+
|
|
32
|
+
/** Provides a custom retry policy to every Mux API call below it. */
|
|
33
|
+
export const policy: {
|
|
34
|
+
(
|
|
35
|
+
options: Options,
|
|
36
|
+
): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Retry>>;
|
|
37
|
+
(
|
|
38
|
+
factory: Factory,
|
|
39
|
+
): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Retry>>;
|
|
40
|
+
} = (optionsOrFactory: Options | Factory) => Effect.provide(Layer.succeed(Retry, optionsOrFactory));
|
|
41
|
+
|
|
42
|
+
/** Disables all automatic retries. */
|
|
43
|
+
export const none: <A, E, R>(
|
|
44
|
+
effect: Effect.Effect<A, E, R>,
|
|
45
|
+
) => Effect.Effect<A, E, Exclude<R, Retry>> = /*@__PURE__*/ Effect.provide(
|
|
46
|
+
/*@__PURE__*/ Layer.succeed(Retry, { while: () => false }),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The default retry policy (core's): transient/throttling/retryable errors,
|
|
51
|
+
* capped exponential backoff with jitter, server `retryAfter` hints honored
|
|
52
|
+
* with precedence.
|
|
53
|
+
*/
|
|
54
|
+
export const makeDefault: Factory = Retries.makeDefault;
|
|
55
|
+
|
|
56
|
+
export const jittered = Retries.jittered;
|
|
57
|
+
export const capped = Retries.capped;
|
|
58
|
+
|
|
59
|
+
/** Retry options that retry all throttling errors indefinitely. */
|
|
60
|
+
export const throttlingOptions: Options = Retries.throttlingOptions;
|
|
61
|
+
|
|
62
|
+
/** Retries all throttling errors indefinitely (honoring server hints). */
|
|
63
|
+
export const throttling: <A, E, R>(
|
|
64
|
+
effect: Effect.Effect<A, E, R>,
|
|
65
|
+
) => Effect.Effect<A, E, Exclude<R, Retry>> = /*@__PURE__*/ policy(Retries.throttlingFactory);
|
|
66
|
+
|
|
67
|
+
/** Retry options that retry all transient errors indefinitely. */
|
|
68
|
+
export const transientOptions: Options = Retries.transientOptions;
|
|
69
|
+
|
|
70
|
+
/** Retries all transient errors indefinitely. */
|
|
71
|
+
export const transient: <A, E, R>(
|
|
72
|
+
effect: Effect.Effect<A, E, R>,
|
|
73
|
+
) => Effect.Effect<A, E, Exclude<R, Retry>> = /*@__PURE__*/ policy(Retries.transientFactory);
|