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
32 changes: 16 additions & 16 deletions examples/blog/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ const resolveIncluded = (article: Article, include: ReadonlyArray<string> | unde
included.push(store.people.get(author.id)!);
}
if (include?.includes("tags")) {
for (const identifier of article.relationships?.tags.data ?? []) {
Arr.forEach(article.relationships?.tags.data ?? [], (identifier) => {
const tag = store.tags.get(identifier.id);
if (tag !== undefined) included.push(tag);
}
});
}
return included;
};
Expand Down Expand Up @@ -233,17 +233,14 @@ export const ArticlesLive = HttpApiBuilder.group(Api, "articles", (handlers) =>
}

// sort=-createdAt,title
for (const term of (query.sort ?? []).toReversed()) {
articles = Arr.reduce((query.sort ?? []).toReversed(), articles, (sorted, term) => {
const ascending = Order.make<(typeof articles)[number]>((a, b) => {
const left = a.attributes[term.field];
const right = b.attributes[term.field];
return left < right ? -1 : left > right ? 1 : 0;
});
articles = Arr.sort(
articles,
term.direction === "desc" ? Order.flip(ascending) : ascending,
);
}
return Arr.sort(sorted, term.direction === "desc" ? Order.flip(ascending) : ascending);
});

// page[offset]=0&page[limit]=10
const total = articles.length;
Expand All @@ -267,10 +264,13 @@ export const ArticlesLive = HttpApiBuilder.group(Api, "articles", (handlers) =>
})
.handle("create", ({ payload }) => {
const title = payload.data.attributes.title;
for (const existing of store.articles.values()) {
if (existing.attributes.title === title) {
return Effect.fail(new TitleTaken({ title }));
}
if (
Arr.some(
Arr.fromIterable(store.articles.values()),
(existing) => existing.attributes.title === title,
)
) {
return Effect.fail(new TitleTaken({ title }));
}
const id = Article.Id.make(`${store.articles.size + 1}`);
const article = Article.make({
Expand Down Expand Up @@ -431,9 +431,9 @@ export const ArticlesLive = HttpApiBuilder.group(Api, "articles", (handlers) =>
Effect.tap(() => requireComments(payload.data)),
Effect.map((article) => {
const linked = store.articleComments.get(article.id) ?? [];
for (const identifier of payload.data) {
Arr.forEach(payload.data, (identifier) => {
if (!linked.includes(identifier.id)) linked.push(identifier.id);
}
});
store.articleComments.set(article.id, linked);
return Handlers.linkage(
linked.map((id) => Comment.ref(id)),
Expand Down Expand Up @@ -818,12 +818,12 @@ const applyOperation = (
const comment = getComment(draft, lids, remove.ref);
draft.comments.delete(comment.id);
// unlink it from any article's paginated comments relationship
for (const [articleId, ids] of draft.articleComments) {
Arr.forEach(draft.articleComments, ([articleId, ids]) => {
draft.articleComments.set(
articleId,
ids.filter((id) => id !== comment.id),
);
}
});
return Atomic.emptyResult;
}),
Match.exhaustive,
Expand Down
4 changes: 2 additions & 2 deletions examples/blog/test/blog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ describe("blog example: heterogeneous search", () => {

// the union is discriminated by the `type` tag; the endpoint declares
// `fields`, so a client reads each attribute as optional (#322)
for (const result of document.data) {
Arr.forEach(document.data, (result) => {
if (result.type === "articles") {
expectTypeOf(result.attributes.title).toEqualTypeOf<string | undefined>();
} else {
Expand All @@ -664,7 +664,7 @@ describe("blog example: heterogeneous search", () => {
result.type === "articles" ? result.attributes.title : result.attributes.firstName,
),
).toBe(true);
}
});
});

it("filters across both resource types", async () => {
Expand Down
10 changes: 5 additions & 5 deletions examples/blog/test/conformance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ describe("blog: JSON:API conformance", () => {
});

it("every failure names a rule, cites its section and shows the request", () => {
for (const outcome of report.outcomes) {
if (!P.isTagged(outcome, "Failed")) continue;
Arr.forEach(report.outcomes, (outcome) => {
if (!P.isTagged(outcome, "Failed")) return;
expect(outcome.rule.requirement.length).toBeGreaterThan(20);
expect(outcome.rule.reference).toMatch(/^https:\/\//);
for (const finding of outcome.findings) {
Arr.forEach(outcome.findings, (finding) => {
expect(finding.detail).not.toBe("");
expect(finding.expected).not.toBe("");
expect(finding.observed).not.toBe("");
}
}
});
});
});

it("actually probed the api rather than skipping its way to a verdict", () => {
Expand Down
28 changes: 16 additions & 12 deletions examples/github/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,17 +392,17 @@ const resolveIssueIncluded = (
}
}
}
for (const key of ["author", "assignee"] as const) {
Arr.forEach(["author", "assignee"] as const, (key) => {
if (include.includes(key)) {
const ref = issue.relationships?.[key].data;
if (ref != null && store.users.has(ref.id)) included.push(store.users.get(ref.id)!);
}
}
});
if (include.includes("labels")) {
for (const ref of issue.relationships?.labels.data ?? []) {
Arr.forEach(issue.relationships?.labels.data ?? [], (ref) => {
const label = store.labels.get(ref.id);
if (label !== undefined) included.push(label);
}
});
}
return included;
};
Expand All @@ -428,10 +428,10 @@ const resolvePullIncluded = (
if (ref !== undefined && store.users.has(ref.id)) included.push(store.users.get(ref.id)!);
}
if (include.includes("reviewers")) {
for (const ref of pull.relationships?.reviewers.data ?? []) {
Arr.forEach(pull.relationships?.reviewers.data ?? [], (ref) => {
const reviewer = store.users.get(ref.id);
if (reviewer !== undefined) included.push(reviewer);
}
});
}
return included;
};
Expand All @@ -441,7 +441,7 @@ const sortBy = <A extends { readonly attributes: any }>(
items: Array<A>,
sort: ReadonlyArray<{ readonly field: string; readonly direction: "asc" | "desc" }> | undefined,
): Array<A> => {
for (const term of (sort ?? []).toReversed()) {
Arr.forEach((sort ?? []).toReversed(), (term) => {
const ascending = Order.make<A>((a, b) => {
const left = a.attributes[term.field];
const right = b.attributes[term.field];
Expand All @@ -453,7 +453,7 @@ const sortBy = <A extends { readonly attributes: any }>(
items.length,
...Arr.sort(items, term.direction === "desc" ? Order.flip(ascending) : ascending),
);
}
});
return items;
};

Expand Down Expand Up @@ -726,10 +726,14 @@ export const RepositoriesLive = HttpApiBuilder.group(Api, "repositories", (handl
const name = payload.data.attributes.name;
// `owner` is a required (`one`) relationship — the payload always carries it.
const owner = payload.data.relationships.owner.data.id;
for (const existing of store.repositories.values()) {
if (existing.attributes.name === name && existing.relationships?.owner.data.id === owner) {
return Effect.fail(new RepositoryNameTaken({ name }));
}
if (
Arr.some(
Arr.fromIterable(store.repositories.values()),
(existing) =>
existing.attributes.name === name && existing.relationships?.owner.data.id === owner,
)
) {
return Effect.fail(new RepositoryNameTaken({ name }));
}
const repository = Repository.make({
id: Repository.Id.make(`${store.repositories.size + 1}`),
Expand Down
10 changes: 5 additions & 5 deletions examples/github/test/conformance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ describe("github: JSON:API conformance", () => {
});

it("every failure names a rule, cites its section and shows the request", () => {
for (const outcome of report.outcomes) {
if (!P.isTagged(outcome, "Failed")) continue;
Arr.forEach(report.outcomes, (outcome) => {
if (!P.isTagged(outcome, "Failed")) return;
expect(outcome.rule.requirement.length).toBeGreaterThan(20);
expect(outcome.rule.reference).toMatch(/^https:\/\//);
for (const finding of outcome.findings) {
Arr.forEach(outcome.findings, (finding) => {
expect(finding.detail).not.toBe("");
expect(finding.expected).not.toBe("");
expect(finding.observed).not.toBe("");
}
}
});
});
});

it("actually probed the api rather than skipping its way to a verdict", () => {
Expand Down
8 changes: 4 additions & 4 deletions examples/github/test/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,10 @@ describe("github example: listing", () => {
bugIssue.attributes.title,
]);
// the closed attribute set decodes to its literal type
for (const issue of document.data) {
Arr.forEach(document.data, (issue) => {
expectTypeOf(issue.attributes.state).toEqualTypeOf<"open" | "closed">();
expect(issue.attributes.state).toBe("open");
}
});
});

it("evaluates an issue filter three-valued: an unassigned issue matches neither eq nor ne", async () => {
Expand Down Expand Up @@ -936,15 +936,15 @@ describe("github example: heterogeneous search", () => {

// the union is discriminated by the `type` tag; the endpoint declares
// `fields`, so a client reads each attribute as optional (#322)
for (const result of document.data) {
Arr.forEach(document.data, (result) => {
if (result.type === "repositories") {
expectTypeOf(result.attributes.stargazerCount).toEqualTypeOf<number | undefined>();
} else if (result.type === "issues") {
expectTypeOf(result.attributes.state).toEqualTypeOf<"open" | "closed" | undefined>();
} else {
expectTypeOf(result.attributes.login).toEqualTypeOf<string | undefined>();
}
}
});
});

it("filters across all resource types", async () => {
Expand Down
4 changes: 2 additions & 2 deletions examples/northwind/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ const sortBy = <A extends { readonly attributes: any }>(
items: Array<A>,
sort: ReadonlyArray<{ readonly field: string; readonly direction: "asc" | "desc" }> | undefined,
): Array<A> => {
for (const term of (sort ?? []).toReversed()) {
Arr.forEach((sort ?? []).toReversed(), (term) => {
const ascending = Ord.make<A>((a, b) => {
const left = a.attributes[term.field];
const right = b.attributes[term.field];
Expand All @@ -544,7 +544,7 @@ const sortBy = <A extends { readonly attributes: any }>(
items.length,
...Arr.sort(items, term.direction === "desc" ? Ord.flip(ascending) : ascending),
);
}
});
return items;
};

Expand Down
10 changes: 5 additions & 5 deletions examples/northwind/test/conformance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,16 @@ describe("northwind: JSON:API conformance", () => {
});

it("every failure names a rule, cites its section and shows the request", () => {
for (const outcome of report.outcomes) {
if (!P.isTagged(outcome, "Failed")) continue;
Arr.forEach(report.outcomes, (outcome) => {
if (!P.isTagged(outcome, "Failed")) return;
expect(outcome.rule.requirement.length).toBeGreaterThan(20);
expect(outcome.rule.reference).toMatch(/^https:\/\//);
for (const finding of outcome.findings) {
Arr.forEach(outcome.findings, (finding) => {
expect(finding.detail).not.toBe("");
expect(finding.expected).not.toBe("");
expect(finding.observed).not.toBe("");
}
}
});
});
});

it("actually probed the api rather than skipping its way to a verdict", () => {
Expand Down
8 changes: 4 additions & 4 deletions examples/northwind/test/northwind.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ describe("northwind example: listing & filtering", () => {

// only order 10249 has no shipped date
expect(document.data.map((order) => order.id)).toContain(order10249.id);
for (const order of document.data) {
Arr.forEach(document.data, (order) => {
expect(order.attributes.shippedDate).toBeUndefined();
}
});
});
});

Expand Down Expand Up @@ -823,15 +823,15 @@ describe("northwind example: heterogeneous search", () => {

// The endpoint declares `fields`, so a client reads each attribute as
// optional (#322).
for (const result of document.data) {
Arr.forEach(document.data, (result) => {
if (result.type === "products") {
expectTypeOf(result.attributes.unitPrice).toEqualTypeOf<number | undefined>();
} else if (result.type === "customers") {
expectTypeOf(result.attributes.companyName).toEqualTypeOf<string | undefined>();
} else {
expectTypeOf(result.attributes.country).toEqualTypeOf<string | undefined>();
}
}
});
});

it("supports include across the searched resources' graphs", async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/Assemble.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* relationship document.
*/
import { it } from "@effect/vitest";
import * as Arr from "effect/Array";
import * as DateTime from "effect/DateTime";
import { describe, expect } from "vite-plus/test";
import * as Effect from "effect/Effect";
Expand Down Expand Up @@ -485,9 +486,9 @@ describe("Assemble.collection", () => {
{ totalCount: 3, offset: 0, limit: 10, baseUrl },
{ viewer: ADMIN, capabilities: familyCapabilities },
);
for (const resource of document.data) {
Arr.forEach(document.data, (resource) => {
expect(resource.meta).toEqual({ canEdit: true, canDelete: true });
}
});
}),
);

Expand Down
9 changes: 5 additions & 4 deletions src/Atomic.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, expectTypeOf, it } from "vite-plus/test";
import * as Arr from "effect/Array";
import * as Match from "effect/Match";
import * as Result from "effect/Result";
import * as Schema from "effect/Schema";
Expand Down Expand Up @@ -304,7 +305,7 @@ describe("relationship operation schemas", () => {
});

it("decodes `many` relationship add / update / remove", () => {
for (const op of ["add", "update", "remove"]) {
Arr.forEach(["add", "update", "remove"], (op) => {
const operation = decodeOperation({
op,
ref: { type: "articles", id: "1", relationship: "comments" },
Expand All @@ -316,21 +317,21 @@ describe("relationship operation schemas", () => {
expect(operation.op).toBe(op);
expect(operation.ref.relationship).toBe("comments");
expect(operation.data).toHaveLength(2);
}
});
});

it("decodes `paginated` relationship add / update / remove", () => {
// paginated relationships have no inline linkage, but they ARE manageable
// through relationship operations
for (const op of ["add", "update", "remove"]) {
Arr.forEach(["add", "update", "remove"], (op) => {
const operation = decodeOperation({
op,
ref: { type: "articles", id: "1", relationship: "subscribers" },
data: [{ type: "people", id: "9" }],
}) as any;
expect(operation.op).toBe(op);
expect(operation.ref.relationship).toBe("subscribers");
}
});
});

it("rejects relationship refs with unknown relationship names", () => {
Expand Down
9 changes: 5 additions & 4 deletions src/Conformance.sort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { it } from "@effect/vitest";
import { assert, describe, expect } from "vite-plus/test";
import * as Arr from "effect/Array";
import * as Option from "effect/Option";
import * as Effect from "effect/Effect";
import * as P from "effect/Predicate";
import * as Schema from "effect/Schema";
Expand Down Expand Up @@ -135,11 +136,11 @@ const ordered = (
const sorted = Arr.sort(
rows,
orderOf<(typeof rows)[number]>((a, b) => {
for (const { field, sign } of applied) {
const decided = Arr.findFirst(applied, ({ field, sign }) => {
const order = sign * sorter.compare(valueOf(a, field), valueOf(b, field), field);
if (order !== 0) return order;
}
return ascending(a.id, b.id);
return order !== 0 ? Option.some(order) : Option.none();
});
return Option.getOrElse(decided, () => ascending(a.id, b.id));
}),
);
return reversed ? sorted.toReversed() : sorted;
Expand Down
Loading
Loading