import type { HttpContext } from '@adonisjs/core/http'; import type { LucidModel, LucidRow } from '@adonisjs/lucid/types/model'; import type { Links, Meta } from './types.ts'; import type { FilterHandler } from './filters.ts'; import type { JsonApiResourceClass } from './registry.ts'; /** * Describes how a Lucid model serializes into a JSON:API resource object. * * A resource class is optional per model — the registry can auto-derive * everything (type name, attributes, relationships) from Lucid metadata. * Subclass to customize: * * ```ts * export default class ArticleResource extends JsonApiResource
{ * static type = 'articles' * static model = () => Article * * attributes() { * return this.pick(['title', 'body', 'createdAt']) * } * } * ``` */ /** * The single home for the relation-visibility rule: a relation is exposed * unless the model hides it (serializeAs: null) or the resource leaves it * out of exposeRelationships. Shared by include validation and * serialization so the two can never drift apart. */ export declare function isRelationExposed(ResourceClass: { exposeRelationships?: string[]; }, name: string, relation: { serializeAs?: string | null; }): boolean; export declare class JsonApiResource { #private; protected resource: Row; protected ctx?: HttpContext | undefined; /** * The JSON:API resource type. Defaults to the kebab-cased table name of * the model. */ static type?: string; /** * The JSON:API type this resource serializes as. This is the single * home for type derivation, consulted by the registry for models and * rows alike. Override it to compute types under a different scheme * entirely. */ static typeName(this: JsonApiResourceClass): string; /** * The Lucid model this resource describes. Required when registering the * resource explicitly. */ static model?: () => LucidModel; /** * Restrict which Lucid relationships are exposed as JSON:API * relationships. Defaults to every relation defined on the model. */ static exposeRelationships?: string[]; /** * Declared ?filter[...] parameters. Nothing is filterable unless listed * here — see the `filter` helpers (filter.eq, filter.gte, * filter.relation, filter.custom). */ static filters?: Record; /** * For the base resource of a single-table inheritance family, the * concrete resources rows of this model can serialize as. Declaring this * makes relations targeting the base accept any member type on writes, * and switches unloaded belongsTo linkage to links-only (the concrete * type of an unloaded row is unknowable from a bare foreign key). */ static subtypes?: () => JsonApiResourceClass[]; /** * For the base resource of a single-table inheritance family, maps a row * to its concrete resource, typically by reading the discriminator * column. Returning undefined keeps the row on this resource. Consulted * by the registry wherever a row is serialized, so linkage and included * documents carry concrete types. */ static resolveResource?: (row: never) => JsonApiResourceClass | undefined; constructor(resource: Row, ctx?: HttpContext | undefined); get Model(): LucidModel; /** * The resource id. JSON:API requires ids to be strings. */ id(): string; /** * Attribute members of the resource object. Defaults to every serializable * column except the primary key and belongsTo foreign keys (which are * represented as relationships instead, per JSON:API recommendations). */ attributes(): Record; /** * Extra per-resource links, merged over the generated ones. Return * undefined to add nothing. The document builder generates the `self` * link from named routes; keys returned here override it. */ links(): Links | undefined; /** * Per-resource meta. Return undefined to omit. */ meta(): Meta | undefined; /** * Helper to cherry-pick model attributes by their serialized names. */ protected pick(names: string[]): Record; }