Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | 2x 62x 62x 2x 2x 4x 1x 2x 31x 31x 4x 2x 7x 2x 8x 31x 27x 7x 31x 2x 17x 20x 2x 1x 1x 2x 8x 9x 2x 1x 1x 36x 23x 2x 10x 1x | import type { H3Event } from "h3";
import { createError } from "h3";
import { defineCachedFunction, useRuntimeConfig } from "nitropack/runtime";
import { ofetch } from "ofetch";
import { OfetchHttpClient } from "#core/adapters/clients/OfetchHttpClient";
import { contentKey } from "#core/contentKey";
import type {
Entry,
EntryQuery,
EntryResource,
Page,
Query,
Resource,
Term,
TermResource,
} from "#core/domain/content";
import { ContentError, ContentUnavailableError } from "#core/domain/errors";
import type ContentProvider from "#core/ports/ContentProvider";
import type { VendorConfig } from "#core/registry";
import createProvider from "#core/registry";
import type { ContentConfig } from "#nuxt/config";
import { toEntryQuery, toQuery, toSlug } from "./query";
const readVendor = (event: H3Event): VendorConfig => {
const { vendor } = useRuntimeConfig(event).content as ContentConfig;
return vendor;
};
const { cache } = useRuntimeConfig().content as ContentConfig;
const rethrowAsHttpError = (cause: unknown): never => {
if (cause instanceof ContentError) throw createError(cause);
throw cause;
};
const resolveProvider = async (vendor: VendorConfig): Promise<ContentProvider> => {
try {
return await createProvider(
vendor,
new OfetchHttpClient(ofetch.create({ baseURL: vendor.baseURL })),
);
} catch (cause) {
return rethrowAsHttpError(cause);
}
};
const throwUnavailableError = (cause: unknown, resource: string): never => {
Eif (cause instanceof ContentError) throw createError(cause);
throw createError(new ContentUnavailableError(resource, cause));
};
type Window = keyof ContentConfig["cache"];
/** One cached reader per kind and shape, so each returns its own type rather than a union to cast. */
const cachedRead = <R extends Resource, A, T>(
name: string,
window: Window,
read: (provider: ContentProvider, resource: R, arg: A) => Promise<T>,
keyQuery: (arg: A) => EntryQuery,
) =>
defineCachedFunction(
async (event: H3Event, resource: R, arg: A): Promise<T> => {
const provider = await resolveProvider(readVendor(event));
return read(provider, resource, arg).catch((cause) =>
throwUnavailableError(cause, resource),
);
},
{
name,
group: "content",
...cache[window],
getKey: (event, resource, arg) =>
contentKey(readVendor(event), resource, keyQuery(arg)),
},
);
const readEntries = cachedRead(
"content-entry-list",
"list",
(provider, resource: EntryResource, query: EntryQuery): Promise<Page<Entry>> =>
provider.listEntries(resource, query),
(query) => query,
);
const readTerms = cachedRead(
"content-term-list",
"list",
(provider, resource: TermResource, query: Query): Promise<Page<Term>> =>
provider.listTerms(resource, query),
(query) => query,
);
const readEntry = cachedRead(
"content-entry",
"item",
(provider, resource: EntryResource, slug: string): Promise<Entry> =>
provider.getEntry(resource, slug),
(slug) => ({ slug }),
);
const readTerm = cachedRead(
"content-term",
"item",
(provider, resource: TermResource, slug: string): Promise<Term> =>
provider.getTerm(resource, slug),
(slug) => ({ slug }),
);
// Each read awaits, so a malformed argument rejects like a failed read instead of throwing
export const useContent = (event: H3Event) => ({
listEntries: async (resource: EntryResource, query?: EntryQuery): Promise<Page<Entry>> =>
await readEntries(event, resource, toEntryQuery(query)),
listTerms: async (resource: TermResource, query?: Query): Promise<Page<Term>> =>
await readTerms(event, resource, toQuery(query)),
getEntry: async (resource: EntryResource, slug: string): Promise<Entry> =>
await readEntry(event, resource, toSlug(slug)),
getTerm: async (resource: TermResource, slug: string): Promise<Term> =>
await readTerm(event, resource, toSlug(slug)),
});
|