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
5 changes: 5 additions & 0 deletions .changeset/open-dictionary-readonly-record.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thomasfosterau/effect-jsonapi": patch
---

The open `meta`, `attributes`, `relationships` and `links` objects of the wire-shape types (`Assemble.WireResource`, `Assemble.ResourceDocument`, `Handlers.MetaValue`, `Client` create/update inputs, `Client.readTotalCount`, …) and `Filter.FieldTypes` are now spelled `ReadonlyRecord<string, unknown>` from `effect/Record` instead of a hand-written index signature. The types are identical, so no migration is needed; `Filter.FieldTypes` is now a type alias rather than an interface.
5 changes: 3 additions & 2 deletions src/ApiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import * as Arr from "effect/Array";
import * as P from "effect/Predicate";
import * as Schema from "effect/Schema";
import * as SchemaTransformation from "effect/SchemaTransformation";
import * as Types from "effect/Types";
import { HttpApiSchema } from "effect/http-api";
import { ErrorDocument, ErrorObject } from "./Document.js";
import type { ErrorSource } from "./Document.js";
Expand Down Expand Up @@ -248,7 +249,7 @@ const makeWire = (
decode: (doc) => {
const first = doc.errors[0];
const meta = first?.meta ?? {};
const fields: Record<string, unknown> = {};
const fields: Types.Mutable<R.ReadonlyRecord<string, unknown>> = {};
Arr.forEach(options.fieldKeys, (key) => {
if (key in meta) fields[key] = meta[key];
else if (key === "detail" && first?.detail !== undefined) fields[key] = first.detail;
Expand All @@ -259,7 +260,7 @@ const makeWire = (
// 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 fields = encoded as R.ReadonlyRecord<string, unknown>;
const detail = options.detail?.(fields);
const source = options.source?.(fields);
return {
Expand Down
18 changes: 9 additions & 9 deletions src/Assemble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export type LinkLike = string | URL | { readonly href: string | URL };
export interface RelationshipObject {
readonly data?: Ref | ReadonlyArray<Ref> | null | undefined;
readonly links?: { readonly related?: LinkLike; readonly self?: LinkLike } | undefined;
readonly meta?: { readonly [key: string]: unknown } | undefined;
readonly meta?: R.ReadonlyRecord<string, unknown> | undefined;
}

/**
Expand All @@ -110,10 +110,10 @@ export interface RelationshipObject {
export interface ResourceLike {
readonly type: string;
readonly id: string;
readonly attributes: { readonly [key: string]: unknown };
readonly relationships?: { readonly [key: string]: unknown } | undefined;
readonly attributes: R.ReadonlyRecord<string, unknown>;
readonly relationships?: R.ReadonlyRecord<string, unknown> | undefined;
readonly links?: { readonly self?: LinkLike | undefined } | undefined;
readonly meta?: { readonly [key: string]: unknown } | undefined;
readonly meta?: R.ReadonlyRecord<string, unknown> | undefined;
}

/**
Expand Down Expand Up @@ -167,10 +167,10 @@ export const JSONAPI_MEMBER = { version: "1.1" } as const;
* @category models
*/
export interface ResourceDocument<R extends ResourceLike, I extends ResourceLike = ResourceLike> {
readonly data: WireResource<R> & { readonly meta?: { readonly [key: string]: unknown } };
readonly data: WireResource<R> & { readonly meta?: R.ReadonlyRecord<string, unknown> };
readonly included?: ReadonlyArray<WireResource<I>>;
readonly links?: { readonly self?: string };
readonly meta?: { readonly [key: string]: unknown };
readonly meta?: R.ReadonlyRecord<string, unknown>;
readonly jsonapi: typeof JSONAPI_MEMBER;
}

Expand All @@ -183,11 +183,11 @@ export interface ResourceDocument<R extends ResourceLike, I extends ResourceLike
*/
export interface CollectionDocument<R extends ResourceLike, I extends ResourceLike = ResourceLike> {
readonly data: ReadonlyArray<
WireResource<R> & { readonly meta?: { readonly [key: string]: unknown } }
WireResource<R> & { readonly meta?: R.ReadonlyRecord<string, unknown> }
>;
readonly included?: ReadonlyArray<WireResource<I>>;
readonly links: Handlers.LinksValue;
readonly meta: { readonly totalCount: number; readonly [key: string]: unknown };
readonly meta: { readonly totalCount: number } & R.ReadonlyRecord<string, unknown>;
readonly jsonapi: typeof JSONAPI_MEMBER;
}

Expand Down Expand Up @@ -266,7 +266,7 @@ export interface RelationshipDocument<R extends ResourceLike, I extends Resource
export type CapabilitiesHook<V = unknown> = (
resource: ResourceLike,
viewer: V,
) => { readonly [key: string]: unknown };
) => R.ReadonlyRecord<string, unknown>;

/**
* The capability rule of a family whose writes gate on a role rung:
Expand Down
22 changes: 11 additions & 11 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,10 +827,10 @@ export const failWithErrorResponse = <A, E, R>(
export interface IncludedResource {
readonly type: string;
readonly id: string;
readonly attributes?: { readonly [key: string]: unknown } | undefined;
readonly relationships?: { readonly [key: string]: unknown } | undefined;
readonly links?: { readonly [key: string]: unknown } | undefined;
readonly meta?: { readonly [key: string]: unknown } | undefined;
readonly attributes?: R.ReadonlyRecord<string, unknown> | undefined;
readonly relationships?: R.ReadonlyRecord<string, unknown> | undefined;
readonly links?: R.ReadonlyRecord<string, unknown> | undefined;
readonly meta?: R.ReadonlyRecord<string, unknown> | undefined;
}

/**
Expand All @@ -841,8 +841,8 @@ export interface IncludedResource {
* @category models
*/
export interface DocumentMembers {
readonly meta?: { readonly [key: string]: unknown } | undefined;
readonly links?: { readonly [key: string]: unknown } | undefined;
readonly meta?: R.ReadonlyRecord<string, unknown> | undefined;
readonly links?: R.ReadonlyRecord<string, unknown> | undefined;
readonly jsonapi?: JsonApiObject | undefined;
}

Expand Down Expand Up @@ -888,10 +888,10 @@ export interface CollectionDocument<R, Included = IncludedResource> extends Docu
*/
export const readTotalCount = (
source:
| {
readonly meta?: { readonly [key: string]: unknown } | undefined;
readonly [member: string]: unknown;
}
| ({ readonly meta?: R.ReadonlyRecord<string, unknown> | undefined } & R.ReadonlyRecord<
string,
unknown
>)
| undefined,
): number => {
const totalCount = source?.meta?.["totalCount"];
Expand Down Expand Up @@ -1017,7 +1017,7 @@ export const indexIncluded = (
* @category models
*/
export interface LinkedResource {
readonly relationships?: { readonly [key: string]: unknown } | undefined;
readonly relationships?: R.ReadonlyRecord<string, unknown> | undefined;
}

/** The refs one relationship key carries: `[]` for absent, `null` or empty linkage. */
Expand Down
3 changes: 2 additions & 1 deletion src/Conformance.sort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as Option from "effect/Option";
import * as Effect from "effect/Effect";
import * as P from "effect/Predicate";
import * as Schema from "effect/Schema";
import * as R from "effect/Record";
import { HttpClient, HttpClientResponse } from "effect/http";
import { HttpApi } from "effect/http-api";
import * as Conformance from "./Conformance.js";
Expand Down Expand Up @@ -52,7 +53,7 @@ const unpagedApi = HttpApi.make("unpaged").add(
type Wire = {
readonly type: "tasks";
readonly id: string;
readonly attributes: Readonly<Record<string, unknown>>;
readonly attributes: R.ReadonlyRecord<string, unknown>;
};

/**
Expand Down
18 changes: 9 additions & 9 deletions src/Conformance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ const conformantServe = (request: {
};
}

const project = (type: string, attributes: Readonly<Record<string, unknown>>) => {
const project = (type: string, attributes: R.ReadonlyRecord<string, unknown>) => {
const requested = params.get(`fields[${type}]`);
if (requested === null) return attributes;
const keep = new Set(requested.split(","));
Expand Down Expand Up @@ -1207,7 +1207,7 @@ const broken = (

const withData = (
canned: Canned,
change: (body: Record<string, unknown>) => Record<string, unknown>,
change: (body: R.ReadonlyRecord<string, unknown>) => R.ReadonlyRecord<string, unknown>,
): Canned => {
const body = canned.body;
if (!P.isObjectOrArray(body) || !("data" in body)) return canned;
Expand Down Expand Up @@ -1571,7 +1571,7 @@ describe("Conformance: detects a deliberately broken server", () => {
);

// #494: a relationship is a field, so a fieldset that leaves it out drops it.
const withAuthor = (body: Record<string, unknown>) => {
const withAuthor = (body: R.ReadonlyRecord<string, unknown>) => {
const data = body["data"];
return P.isObjectOrArray(data) && "type" in data && data.type === "articles"
? {
Expand Down Expand Up @@ -6120,8 +6120,8 @@ describe("Conformance: the write fixture", () => {
(canned, request) =>
creates(request, canned)
? withData(canned, (body) => {
const data = body["data"] as Record<string, unknown>;
const attributes = data["attributes"] as Record<string, unknown>;
const data = body["data"] as R.ReadonlyRecord<string, unknown>;
const attributes = data["attributes"] as R.ReadonlyRecord<string, unknown>;
return { ...body, data: { ...data, attributes: { ...attributes, rating: 5 } } };
})
: canned,
Expand All @@ -6135,8 +6135,8 @@ describe("Conformance: the write fixture", () => {
(canned, request) =>
request.method === "PATCH" && canned.status === undefined
? withData(canned, (body) => {
const data = body["data"] as Record<string, unknown>;
const attributes = data["attributes"] as Record<string, unknown>;
const data = body["data"] as R.ReadonlyRecord<string, unknown>;
const attributes = data["attributes"] as R.ReadonlyRecord<string, unknown>;
return { ...body, data: { ...data, attributes: { ...attributes, title: "unstored" } } };
})
: canned,
Expand All @@ -6160,7 +6160,7 @@ describe("Conformance: the write fixture", () => {
}
return request.method === "GET" && patched.has(id)
? withData(canned, (body) => {
const data = body["data"] as Record<string, unknown>;
const data = body["data"] as R.ReadonlyRecord<string, unknown>;
return { ...body, data: { ...data, attributes: { title: "unstored" } } };
})
: canned;
Expand Down Expand Up @@ -6268,7 +6268,7 @@ describe("Conformance: atomic operations processing", () => {

const atomicType = `${MEDIA_TYPE};ext="${ATOMIC_EXTENSION_URI}"`;

type Fields = Readonly<Record<string, unknown>>;
type Fields = R.ReadonlyRecord<string, unknown>;
interface Stored {
readonly type: string;
readonly id: string;
Expand Down
3 changes: 2 additions & 1 deletion src/Endpoint.conflict.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { describe, expect, it } from "vite-plus/test";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import * as R from "effect/Record";
import { HttpRouter } from "effect/http";
import { HttpApi, HttpApiBuilder } from "effect/http-api";
import * as Endpoint from "./Endpoint.js";
Expand Down Expand Up @@ -83,7 +84,7 @@ const decodeJson = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Any));
const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Any));

// A JSON request body: a document, or a flat command input.
type RequestBody = Readonly<Record<string, unknown>>;
type RequestBody = R.ReadonlyRecord<string, unknown>;

const send = async (
method: "POST" | "PATCH",
Expand Down
8 changes: 4 additions & 4 deletions src/Endpoint.sparse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const wire = {
} as const;

// A wire resource object with only the named attributes.
const only = <O extends { readonly attributes: Readonly<Record<string, unknown>> }>(
const only = <O extends { readonly attributes: R.ReadonlyRecord<string, unknown> }>(
object: O,
...names: ReadonlyArray<keyof O["attributes"] & string>
) => ({
Expand All @@ -121,8 +121,8 @@ const only = <O extends { readonly attributes: Readonly<Record<string, unknown>>
// when it names none of them.
const projected = <
O extends {
readonly attributes: Readonly<Record<string, unknown>>;
readonly relationships?: Readonly<Record<string, unknown>>;
readonly attributes: R.ReadonlyRecord<string, unknown>;
readonly relationships?: R.ReadonlyRecord<string, unknown>;
},
>(
object: O,
Expand All @@ -144,7 +144,7 @@ const successOf = (endpoint: { readonly success: Iterable<Schema.Top> }) =>
[...endpoint.success][0] as Schema.Codec<unknown, unknown>;

// A wire resource object or document, as a client receives it.
type Wire = Readonly<Record<string, unknown>>;
type Wire = R.ReadonlyRecord<string, unknown>;

// Whether an endpoint's success document decodes `document`.
const decodes = (endpoint: { readonly success: Iterable<Schema.Top> }, document: Wire) =>
Expand Down
27 changes: 17 additions & 10 deletions src/Endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import * as Arr from "effect/Array";
import * as Option from "effect/Option";
import * as P from "effect/Predicate";
import * as Schema from "effect/Schema";
import * as Types from "effect/Types";
import { HttpApiEndpoint, HttpApiSchema } from "effect/http-api";
import * as Atomic from "./Atomic.js";
import { AnyMeta, CollectionDocument, DataDocument, LinkageDocument, wire } from "./Document.js";
Expand Down Expand Up @@ -4095,7 +4096,7 @@ const pick = <C extends object, K extends keyof C & string, F>(
// gave it.
interface OpConfig {
readonly emit: boolean;
readonly config: Record<string, unknown>;
readonly config: R.ReadonlyRecord<string, unknown>;
}

/**
Expand Down Expand Up @@ -4241,7 +4242,7 @@ export const resource = <
const gMeta = options?.meta;
const gErrors = options?.errors;
const gIdem = options?.idempotency;
const endpointsOpt = options?.endpoints as Record<string, unknown> | undefined;
const endpointsOpt = options?.endpoints as R.ReadonlyRecord<string, unknown> | undefined;
const relationshipsOpt = options?.relationships ?? true;

// The resource's base meta schema, for resolving `meta` functions.
Expand All @@ -4259,12 +4260,12 @@ export const resource = <
const value = endpointsOpt[op];
if (value === undefined || value === true) return { emit: true, config: {} };
if (value === false) return { emit: false, config: {} };
return { emit: true, config: value as Record<string, unknown> };
return { emit: true, config: value as R.ReadonlyRecord<string, unknown> };
};

// The shared name / path / errors (and, optionally, meta) overrides of an op.
const commonOpts = (config: Record<string, unknown>, withMeta: boolean) => {
const out: Record<string, unknown> = {};
const commonOpts = (config: R.ReadonlyRecord<string, unknown>, withMeta: boolean) => {
const out: Types.Mutable<R.ReadonlyRecord<string, unknown>> = {};
const errors = pick(config, "errors", gErrors);
if (errors !== undefined) out.errors = errors;
if ("name" in config) out.name = config.name;
Expand All @@ -4282,19 +4283,23 @@ export const resource = <
// An endpoint's `success` override is per-endpoint only: the primary-data
// endpoints answer with a single-resource document and `list` with a
// collection, so there is no sensible top-level default to fall back to.
const successOpt = (config: Record<string, unknown>): Record<string, unknown> =>
const successOpt = (
config: R.ReadonlyRecord<string, unknown>,
): R.ReadonlyRecord<string, unknown> =>
config.success !== undefined ? { success: config.success } : {};

// Likewise `status`: each endpoint's default (201 for `create`, 200 for
// `update`, 204 for `delete`) is the constructor's, so an absent key must
// stay absent rather than resolve to some shared number here.
const statusOpt = (config: Record<string, unknown>): Record<string, unknown> =>
const statusOpt = (
config: R.ReadonlyRecord<string, unknown>,
): R.ReadonlyRecord<string, unknown> =>
config.status !== undefined ? { status: config.status } : {};

// A read endpoint's `query` override is per-endpoint only: it replaces that
// endpoint's whole composed query, and `get`'s and `list`'s compositions
// differ, so there is no sensible top-level default to fall back to.
const queryOpt = (config: Record<string, unknown>): Record<string, unknown> =>
const queryOpt = (config: R.ReadonlyRecord<string, unknown>): R.ReadonlyRecord<string, unknown> =>
config.query !== undefined ? { query: config.query } : {};

const endpoints: Array<HttpApiEndpoint.Top> = [];
Expand Down Expand Up @@ -4333,12 +4338,14 @@ export const resource = <
// top-level default to fall back to.
// `idempotency` is the one write option with a top-level default, so an
// absent per-endpoint key inherits it rather than staying absent.
const idempotencyOpt = (config: Record<string, unknown>): Record<string, unknown> => {
const idempotencyOpt = (
config: R.ReadonlyRecord<string, unknown>,
): R.ReadonlyRecord<string, unknown> => {
const idempotency = pick(config, "idempotency", gIdem);
return idempotency !== undefined ? { idempotency } : {};
};

const payloadOpt = (config: Record<string, unknown>) => ({
const payloadOpt = (config: R.ReadonlyRecord<string, unknown>) => ({
...definedMember("payload", config.payload),
...definedMember("payloadMediaType", config.payloadMediaType),
});
Expand Down
5 changes: 2 additions & 3 deletions src/Filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @since 0.13.0
*/
import * as Schema from "effect/Schema";
import * as R from "effect/Record";
import type { FieldCodecs } from "./internal/filter.js";
import { literalEncoder, operatorCheck, prepare } from "./internal/filter.js";
import {
Expand Down Expand Up @@ -361,9 +362,7 @@ export type CompareOperator = (typeof compareOperators)[number];
* @since 0.13.0
* @category models
*/
export interface FieldTypes {
readonly [field: string]: unknown;
}
export type FieldTypes = R.ReadonlyRecord<string, unknown>;

/**
* A comparison of one field against one typed literal.
Expand Down
Loading
Loading