Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: type PrismicDocument.*_publication_date as TimestampField<"filled"> #304

Merged
merged 4 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
242 changes: 112 additions & 130 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"imgix-url-builder": "^0.0.3"
},
"devDependencies": {
"@prismicio/mock": "^0.2.0",
"@prismicio/mock": "^0.3.0",
"@prismicio/types-internal": "2.0.0-alpha.11",
"@size-limit/preset-small-lib": "^8.2.4",
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
Expand Down
5 changes: 3 additions & 2 deletions src/types/value/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AnyRegularField } from "./types";

import { GroupField } from "./group";
import { SliceZone } from "./sliceZone";
import { TimestampField } from "./timestamp";

/**
* Document metadata for a translation of a Prismic document.
Expand Down Expand Up @@ -46,11 +47,11 @@ export interface PrismicDocumentHeader<TypeEnum = string, LangEnum = string> {
/**
* The timestamp at which the document was first published.
*/
first_publication_date: string;
first_publication_date: TimestampField<"filled">;
/**
* The timestamp at which the document was last published.
*/
last_publication_date: string;
last_publication_date: TimestampField<"filled">;
/**
* Slugs associated with document.
*
Expand Down
1 change: 1 addition & 0 deletions test/__testutils__/createPagedQueryResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export const createPagedQueryResponses = ({
);
}

// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
return responses;
};
2 changes: 2 additions & 0 deletions test/__testutils__/testFetchOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const testFetchOptions = (
mockPrismicRestAPIV2({
ctx,
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse: ctx.mock.api.query({
documents: [ctx.mock.value.document()],
}),
Expand Down Expand Up @@ -81,6 +82,7 @@ export const testFetchOptions = (
mockPrismicRestAPIV2({
ctx,
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse: ctx.mock.api.query({
documents: [ctx.mock.value.document()],
}),
Expand Down
3 changes: 3 additions & 0 deletions test/__testutils__/testGetTTL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const testGetWithinTTL = (

mockPrismicRestAPIV2({
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref,
Expand Down Expand Up @@ -105,6 +106,7 @@ export const testGetOutsideTTL = (

mockPrismicRestAPIV2({
repositoryResponse: repositoryResponse1,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse: queryResponse1,
queryRequiredParams: {
ref: ref1,
Expand All @@ -129,6 +131,7 @@ export const testGetOutsideTTL = (
// We're also using a new query handler using the new master ref.
mockPrismicRestAPIV2({
repositoryResponse: repositoryResponse2,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse: queryResponse2,
queryRequiredParams: {
ref: ref2,
Expand Down
1 change: 1 addition & 0 deletions test/client-queryContentFromRef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ it("uses master ref if manual thunk ref returns non-string value", async (ctx) =

mockPrismicRestAPIV2({
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref: getMasterRef(repositoryResponse),
Expand Down
1 change: 1 addition & 0 deletions test/client-queryContentFromReleaseById.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ it("uses a releases ref by ID", async (ctx) => {

mockPrismicRestAPIV2({
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref: ref1.ref,
Expand Down
1 change: 1 addition & 0 deletions test/client-queryContentFromReleaseByLabel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ it("uses a releases ref by label", async (ctx) => {

mockPrismicRestAPIV2({
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref: ref1.ref,
Expand Down
6 changes: 6 additions & 0 deletions test/client-resolvePreviewUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ it("resolves a preview url in the browser", async (ctx) => {
};

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref: previewToken,
Expand Down Expand Up @@ -57,6 +58,7 @@ it("resolves a preview url using a server req object", async (ctx) => {
};

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref: previewToken,
Expand Down Expand Up @@ -92,6 +94,7 @@ it("resolves a preview url using a Web API-based server req object", async (ctx)
};

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref: previewToken,
Expand Down Expand Up @@ -125,6 +128,7 @@ it("resolves a preview url using a Web API-based server req object containing a
};

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref: previewToken,
Expand Down Expand Up @@ -158,6 +162,7 @@ it("allows providing an explicit documentId and previewToken", async (ctx) => {
};

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref: previewToken,
Expand Down Expand Up @@ -235,6 +240,7 @@ it("returns defaultURL if resolved URL is not a string", async (ctx) => {
const defaultURL = "defaultURL";

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref: previewToken,
Expand Down
14 changes: 14 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ it("uses the master ref by default", async (ctx) => {
const queryResponse = prismicM.api.query({ seed: ctx.meta.name });

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
ctx,
});
Expand All @@ -235,6 +236,7 @@ it("supports manual string ref", async (ctx) => {
const ref = "ref";

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: { ref },
ctx,
Expand All @@ -251,6 +253,7 @@ it("supports manual thunk ref", async (ctx) => {
const ref = "ref";

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: { ref },
ctx,
Expand All @@ -268,6 +271,7 @@ it("uses master ref if ref thunk param returns non-string value", async (ctx) =>

mockPrismicRestAPIV2({
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: { ref: getMasterRef(repositoryResponse) },
ctx,
Expand All @@ -289,6 +293,7 @@ it("uses browser preview ref if available", async (ctx) => {
const queryResponse = prismicM.api.query({ seed: ctx.meta.name });

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: { ref: previewRef },
ctx,
Expand All @@ -313,6 +318,7 @@ it("uses req preview ref if available", async (ctx) => {
const queryResponse = prismicM.api.query({ seed: ctx.meta.name });

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: { ref: previewRef },
ctx,
Expand All @@ -337,6 +343,7 @@ it("supports req with Web APIs", async (ctx) => {
const queryResponse = prismicM.api.query({ seed: ctx.meta.name });

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: { ref: previewRef },
ctx,
Expand All @@ -359,6 +366,7 @@ it("ignores req without cookies", async (ctx) => {

mockPrismicRestAPIV2({
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: { ref: getMasterRef(repositoryResponse) },
ctx,
Expand Down Expand Up @@ -389,6 +397,7 @@ it("does not use preview ref if auto previews are disabled", async (ctx) => {
client.disableAutoPreviews();
mockPrismicRestAPIV2({
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: { ref: getMasterRef(repositoryResponse) },
ctx,
Expand All @@ -402,6 +411,7 @@ it("does not use preview ref if auto previews are disabled", async (ctx) => {
client.enableAutoPreviews();
mockPrismicRestAPIV2({
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: { ref: previewRef },
ctx,
Expand All @@ -421,6 +431,7 @@ it("uses the integration fields ref if the repository provides it", async (ctx)

mockPrismicRestAPIV2({
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref: getMasterRef(repositoryResponse),
Expand All @@ -443,6 +454,7 @@ it("ignores the integration fields ref if the repository provides a null value",

mockPrismicRestAPIV2({
repositoryResponse,
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
ref: getMasterRef(repositoryResponse),
Expand Down Expand Up @@ -478,6 +490,7 @@ it("uses client-provided routes in queries", async (ctx) => {
];

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
routes: JSON.stringify(routes),
Expand All @@ -497,6 +510,7 @@ it("uses client-provided brokenRoute in queries", async (ctx) => {
const brokenRoute = "/404";

mockPrismicRestAPIV2({
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
queryResponse,
queryRequiredParams: {
brokenRoute,
Expand Down
15 changes: 15 additions & 0 deletions test/helpers-asDate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect, it } from "vitest";

import { documentFixture } from "./__fixtures__/document";

import { asDate } from "../src";

it("returns null for nullish inputs", () => {
Expand Down Expand Up @@ -30,3 +32,16 @@ it("returns a date object from a timestamp field", () => {

expect(asDate(field)).toBeInstanceOf(Date);
});

it("is compatible with a document's first_publication_date and last_publication_date properties", () => {
// This test is functionally no different than the tests above, but we
// can at least check that the types between `PrismicDocument` and
// `asDate()` are compatible.

expect(asDate(documentFixture.empty.first_publication_date)).toBeInstanceOf(
Date,
);
expect(asDate(documentFixture.empty.last_publication_date)).toBeInstanceOf(
Date,
);
});
15 changes: 13 additions & 2 deletions test/helpers-asLinkAttrs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,23 @@ it("resolves a document", (ctx) => {
const doc = ctx.mock.value.document();
doc.url = "/foo";

expect(asLinkAttrs(doc)).toEqual({
expect(
asLinkAttrs(
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
doc,
),
).toEqual({
href: doc.url,
target: undefined,
rel: undefined,
});
expect(asLinkAttrs(doc, { linkResolver: () => "/linkResolver" })).toEqual({
expect(
asLinkAttrs(
// @ts-expect-error - Remove after publishing https://github.com/prismicio/prismic-client/pull/304
doc,
{ linkResolver: () => "/linkResolver" },
),
).toEqual({
href: "/linkResolver",
target: undefined,
rel: undefined,
Expand Down
14 changes: 10 additions & 4 deletions test/helpers-isFilled.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect, it } from "vitest";

import { documentFixture } from "./__fixtures__/document";

import { GroupField, SliceZone, isFilled } from "../src";

it("color", (ctx) => {
Expand Down Expand Up @@ -78,12 +80,10 @@ it("image thumbnail", () => {
it("integration fields", (ctx) => {
expect(isFilled.integrationField(null)).toBe(false);
expect(isFilled.integrationField(undefined)).toBe(false);
expect(isFilled.integrationField(ctx.mock.value.integrationFields())).toBe(
true,
);
expect(isFilled.integrationField(ctx.mock.value.integration())).toBe(true);
});
it("aliases integrationFields to integrationField", () => {
expect(isFilled.integrationFields).toBe(isFilled.integrationField);
expect(isFilled.integrationField).toBe(isFilled.integrationField);
});

it("key text", (ctx) => {
Expand Down Expand Up @@ -163,6 +163,12 @@ it("timestamp", (ctx) => {
expect(isFilled.timestamp(null)).toBe(false);
expect(isFilled.timestamp(undefined)).toBe(false);
expect(isFilled.timestamp(ctx.mock.value.timestamp())).toBe(true);
expect(isFilled.timestamp(documentFixture.empty.first_publication_date)).toBe(
true,
);
expect(isFilled.timestamp(documentFixture.empty.last_publication_date)).toBe(
true,
);
});

it("title", (ctx) => {
Expand Down
8 changes: 4 additions & 4 deletions test/types/api-query.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ expectType<prismic.Query>({
type: "string",
href: "string",
tags: ["string"],
first_publication_date: "string",
last_publication_date: "string",
first_publication_date: "2020-06-29T15:13:27+0000",
last_publication_date: "2020-06-29T15:13:27+0000",
slugs: ["string"],
linked_documents: [],
lang: "string",
Expand Down Expand Up @@ -91,8 +91,8 @@ expectType<
type: "string",
href: "string",
tags: ["string"],
first_publication_date: "string",
last_publication_date: "string",
first_publication_date: "2020-06-29T15:13:27+0000",
last_publication_date: "2020-06-29T15:13:27+0000",
slugs: ["string"],
linked_documents: [],
lang: "string",
Expand Down
Loading