Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions examples/github/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,9 @@ const loadIssue = (id: string): Effect.Effect<Issue, IssueNotFound> => {

const loadUnlockedIssue = (id: string): Effect.Effect<Issue, IssueNotFound | IssueLocked> =>
loadIssue(id).pipe(
Effect.flatMap((issue) =>
issue.attributes.locked
? Effect.fail(new IssueLocked({ id: issue.id }))
: Effect.succeed(issue),
Effect.filterOrFail(
(issue) => !issue.attributes.locked,
(issue) => new IssueLocked({ id: issue.id }),
),
);

Expand Down
5 changes: 2 additions & 3 deletions examples/github/test/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,8 @@ describe("github example: pagination links carry the request's query (#447)", ()
});
const next = first.links?.next;
assert(Predicate.isString(next));
const second = yield* client.issues.list({
query: Schema.decodeUnknownSync(IssuesQuery)(searchOf(next)),
});
const query = yield* Schema.decodeUnknownEffect(IssuesQuery)(searchOf(next));
const second = yield* client.issues.list({ query });
return [first, second] as const;
}),
);
Expand Down
2 changes: 1 addition & 1 deletion examples/northwind/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ export const ProductsLive = HttpApiBuilder.group(Api, "products", (handlers) =>
Effect.gen(function* () {
const name = payload.data.attributes.name;
if (Iterable.some(store.products.values(), (existing) => existing.attributes.name === name))
return yield* Effect.fail(new ProductNameTaken({ name }));
return yield* new ProductNameTaken({ name });
// `category` and `supplier` are required (`one`) relationships — the
// payload always carries them, and both targets must exist.
yield* loadCategory(payload.data.relationships.category.data.id);
Expand Down
5 changes: 2 additions & 3 deletions examples/northwind/test/northwind.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,8 @@ describe("northwind example: pagination links carry the request's query (#447)",
});
const next = first.links?.next;
assert(Predicate.isString(next));
const second = yield* client.products.list({
query: Schema.decodeUnknownSync(ProductsQuery)(searchOf(next)),
});
const query = yield* Schema.decodeUnknownEffect(ProductsQuery)(searchOf(next));
const second = yield* client.products.list({ query });
return [first, second] as const;
}),
);
Expand Down
109 changes: 54 additions & 55 deletions src/ApiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,61 +232,60 @@ const makeWire = (
}),
) as unknown as typeof WireDocument;

const wire = strictDocument
.pipe(
Schema.decodeTo(
klass as unknown as Schema.Top,
SchemaTransformation.transform<unknown, WireDocumentType>({
// document -> class encoded form ({ _tag, ...fields }). A field the
// document's `meta` does not carry is left absent (not `undefined`),
// so an optional field decodes from a document another server wrote
// without `meta`; a `detail` field falls back to the error object's
// own `detail` — the source-bearing 400s of the schema-error
// middleware carry it there and nowhere else.
decode: (doc) => {
const first = doc.errors[0];
const meta = first?.meta ?? {};
const fields: Record<string, unknown> = {};
for (const key of options.fieldKeys) {
if (key in meta) fields[key] = meta[key];
else if (key === "detail" && first?.detail !== undefined) fields[key] = first.detail;
}
return { _tag: options.tag, ...fields };
},
// class encoded form -> document. An optional field the instance
// leaves absent stays out of `meta` (an `undefined` member is not
// JSON), mirroring `decode` above.
encode: (encoded) => {
const fields = encoded as Record<string, unknown>;
const detail = options.detail?.(fields);
const source = options.source?.(fields);
return {
errors: [
{
status: String(options.status),
code: options.code,
...definedMember("title", options.title),
...definedMember("detail", detail),
...definedMember("source", source),
...definedMember(
"meta",
Arr.isReadonlyArrayNonEmpty(options.fieldKeys)
? Object.fromEntries(
options.fieldKeys.flatMap((key) =>
fields[key] === undefined ? [] : [[key, fields[key]]],
),
)
: undefined,
),
},
],
};
},
}),
),
HttpApiSchema.status(options.status),
)
.pipe((schema) => asJsonApi(schema));
const wire = strictDocument.pipe(
Schema.decodeTo(
klass as unknown as Schema.Top,
SchemaTransformation.transform<unknown, WireDocumentType>({
// document -> class encoded form ({ _tag, ...fields }). A field the
// document's `meta` does not carry is left absent (not `undefined`),
// so an optional field decodes from a document another server wrote
// without `meta`; a `detail` field falls back to the error object's
// own `detail` — the source-bearing 400s of the schema-error
// middleware carry it there and nowhere else.
decode: (doc) => {
const first = doc.errors[0];
const meta = first?.meta ?? {};
const fields: Record<string, unknown> = {};
for (const key of options.fieldKeys) {
if (key in meta) fields[key] = meta[key];
else if (key === "detail" && first?.detail !== undefined) fields[key] = first.detail;
}
return { _tag: options.tag, ...fields };
},
// class encoded form -> document. An optional field the instance
// leaves absent stays out of `meta` (an `undefined` member is not
// JSON), mirroring `decode` above.
encode: (encoded) => {
const fields = encoded as Record<string, unknown>;
const detail = options.detail?.(fields);
const source = options.source?.(fields);
return {
errors: [
{
status: String(options.status),
code: options.code,
...definedMember("title", options.title),
...definedMember("detail", detail),
...definedMember("source", source),
...definedMember(
"meta",
Arr.isReadonlyArrayNonEmpty(options.fieldKeys)
? Object.fromEntries(
options.fieldKeys.flatMap((key) =>
fields[key] === undefined ? [] : [[key, fields[key]]],
),
)
: undefined,
),
},
],
};
},
}),
),
HttpApiSchema.status(options.status),
(schema) => asJsonApi(schema),
);

wireCache.set(klass, wire);
return wire;
Expand Down
2 changes: 1 addition & 1 deletion src/Assemble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ const walk = <Targets extends Handlers.IncludeTargets>(
Handlers.IncludeTargetServices<Targets>
> => {
const include = options.include;
if (include === undefined) return Effect.succeed(undefined);
if (include === undefined) return Effect.undefined;
const depth = options.depth ?? MAX_INCLUDE_DEPTH;
return Query.validateIncludePaths(include, {
includable: options.includable ?? [],
Expand Down
4 changes: 2 additions & 2 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ export const decodeErrorDocument = (
*/
export const errorDocument = (cause: unknown): Effect.Effect<Option.Option<ErrorDocument>> => {
const response = HttpClientError.isHttpClientError(cause) ? cause.response : undefined;
if (response === undefined) return Effect.succeed(Option.none());
if (response === undefined) return Effect.succeedNone;
return response.text.pipe(
// The body may be unreadable (already consumed, a broken stream): that is
// still "no document", never a second failure on top of the first.
Expand Down Expand Up @@ -760,7 +760,7 @@ export const isErrorResponse = (u: unknown): u is ErrorResponse => u instanceof
*/
export const toErrorResponse = (cause: unknown): Effect.Effect<Option.Option<ErrorResponse>> => {
if (!HttpClientError.isHttpClientError(cause) || cause.response === undefined) {
return Effect.succeed(Option.none());
return Effect.succeedNone;
}
const status = cause.response.status;
return errorDocument(cause).pipe(
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ export const resolveIncluded = <Targets extends IncludeTargets>(
IncludeTargetServices<Targets>
> =>
(paths === undefined
? Effect.succeed(undefined)
? Effect.undefined
: Arr.isReadonlyArrayEmpty(paths)
? Effect.succeed([])
: resolve(
Expand Down
6 changes: 2 additions & 4 deletions src/Idempotency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,12 +871,10 @@ export const layer = (store: Store): Layer.Layer<Middleware> =>
// A key already seen: a different request under it is a mismatch;
// the same one is still in flight, or replays its recorded response.
if (Predicate.isTagged(claim, "InProgress")) {
return yield* Effect.fail(
claim.fingerprint !== print ? new KeyMismatch() : new KeyInProgress(),
);
return yield* claim.fingerprint !== print ? new KeyMismatch() : new KeyInProgress();
}
if (Predicate.isTagged(claim, "Completed")) {
if (claim.fingerprint !== print) return yield* Effect.fail(new KeyMismatch());
if (claim.fingerprint !== print) return yield* new KeyMismatch();
return replay(claim.response);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ export const contentNegotiationLayer = (
),
Effect.withSpan(Span.negotiate),
);
if (error !== undefined) return yield* Effect.fail(error);
if (error !== undefined) return yield* error;
return yield* httpEffect;
}),
),
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ export const assertIdMatchesPath = (

const identityTransact: Transact<never, never, never> = (effect) => effect;

const none = Effect.succeed(undefined);
const none = Effect.undefined;

/**
* Builds the `list` / `get` handlers for a read-only resource, keyed by the
Expand Down
2 changes: 1 addition & 1 deletion src/internal/conformance/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ export const DEFAULT_REQUEST_TIMEOUT: Duration.Duration = Duration.seconds(30);
export const requestTimeoutOf = (
given: Option.Option<Duration.Input>,
): Effect.Effect<Option.Option<Duration.Duration>> => {
if (Option.isNone(given)) return Effect.succeed(Option.some(DEFAULT_REQUEST_TIMEOUT));
if (Option.isNone(given)) return Effect.succeedSome(DEFAULT_REQUEST_TIMEOUT);
const input = given.value;
const timeout = Duration.fromInput(input);
if (Option.isSome(timeout) && Duration.isPositive(timeout.value)) {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/conformance/crawl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export const budgeted = (ctx: Context, budget: number): Io => {
const answers = MutableHashMap.empty<string, Observation>();
const refetch = (target: Target): Effect.Effect<Option.Option<Observation>> =>
Effect.suspend(() => {
if (spent >= budget) return Effect.succeed(Option.none());
if (spent >= budget) return Effect.succeedNone;
return ctx
.request({ url: target.url, headers: jsonApiAccept, requests: budget - spent })
.pipe(
Expand All @@ -278,7 +278,7 @@ export const budgeted = (ctx: Context, budget: number): Io => {
Effect.suspend(() =>
Option.match(MutableHashMap.get(answers, target.href), {
onNone: () => refetch(target),
onSome: (observation) => Effect.succeed(Option.some(observation)),
onSome: (observation) => Effect.succeedSome(observation),
}),
),
spent: () => spent,
Expand Down
13 changes: 6 additions & 7 deletions src/internal/conformance/evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -989,15 +989,14 @@ const judgeFields = (
Arr.matchLeft(fields, {
onEmpty: () => Effect.succeed<ReadonlyArray<Judged>>([]),
onNonEmpty: (field, rest) =>
Effect.flatMap(
Effect.filterOrElse(
judgeAll(io, ctx, path, candidates, probesOf(field)),
(judged) => Arr.some(judged, Judged.$is("Decided")),
(judged): Effect.Effect<ReadonlyArray<Judged>> =>
Arr.some(judged, Judged.$is("Decided"))
? Effect.succeed(judged)
: Effect.map(judgeFields(io, ctx, path, candidates, rest, probesOf), (after) => [
...judged,
...after,
]),
Effect.map(judgeFields(io, ctx, path, candidates, rest, probesOf), (after) => [
...judged,
...after,
]),
),
});

Expand Down
9 changes: 4 additions & 5 deletions src/internal/wireNames.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,10 @@ describe("wireProperties", () => {
// The wire is `x`: read as a plain struct, `a` would be paired with `a`.
[
"a transformation from a struct with an encoding of its own",
Schema.Struct({ a: Schema.String })
.pipe(Schema.encodeKeys({ a: "x" }))
.pipe(
Schema.decodeTo(Schema.Struct({ a: Schema.String }), SchemaTransformation.passthrough()),
),
Schema.Struct({ a: Schema.String }).pipe(
Schema.encodeKeys({ a: "x" }),
Schema.decodeTo(Schema.Struct({ a: Schema.String }), SchemaTransformation.passthrough()),
),
],
[
"a transformation from a value that is no struct",
Expand Down
Loading