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
18 changes: 18 additions & 0 deletions projects/api/src/contracts/_internal/request/actionDateSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { z } from '../z.ts';

const keywordSchema = z.enum(['released', 'unknown', 'now']);
const datetimeSchema = z.string().datetime();

/**
* Zod schema for an action date: a UTC datetime, or one of the keywords
* `released`, `unknown` and `now`. A refined string, not a union, so OpenAPI
* emits a single `string` instead of an ambiguous `oneOf`.
*/
export const actionDateSchema = z
.string()
.refine(
(value) =>
keywordSchema.safeParse(value).success ||
datetimeSchema.safeParse(value).success,
{ message: 'Expected a UTC datetime, released, unknown or now' },
);
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { z } from '../z.ts';
import { actionDateSchema } from './actionDateSchema.ts';
import { mediaItemsRequestSchemaFactory } from './mediaItemsRequestSchemaFactory.ts';

/** Zod schema for the history items request. */
export const historyItemsRequestSchema = mediaItemsRequestSchemaFactory({
watched_at: z
.union([z.string().datetime(), z.enum(['released', 'unknown', 'now'])])
.nullish()
.openapi({
description:
'When the item was watched: a UTC datetime, `released` for its release date, `unknown` for no specific date, or `now` for the current time.',
}),
watched_at: actionDateSchema.nullish().openapi({
description:
'When the item was watched: a UTC datetime, `released` for its release date, `unknown` for no specific date, or `now` for the current time.',
}),
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { actionDateSchema } from '../../../_internal/request/actionDateSchema.ts';
import { mediaItemsRequestSchemaFactory } from '../../../_internal/request/mediaItemsRequestSchemaFactory.ts';
import { z } from '../../../_internal/z.ts';

/** Zod schema for the collection add request. */
export const collectionAddRequestSchema = mediaItemsRequestSchemaFactory({
collected_at: z
.union([z.string().datetime(), z.enum(['released', 'unknown', 'now'])])
.nullish()
.openapi({
description:
'When the item was collected: a UTC datetime, `released` for its release date, `unknown` for no specific date, or `now` for the current time. Defaults to the stored date, else now.',
}),
collected_at: actionDateSchema.nullish().openapi({
description:
'When the item was collected: a UTC datetime, `released` for its release date, `unknown` for no specific date, or `now` for the current time. Defaults to the stored date, else now.',
}),
media_type: z
.enum([
'digital',
Expand Down
15 changes: 15 additions & 0 deletions projects/api/src/contracts/users/UserStatsResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,18 @@ test('@types/UserStatsResponse: complete stats response', () => {

expectTypeOf<typeof stats>().toEqualTypeOf<UserStatsResponse>();
});

test('@types/UserStatsResponse: progress, lists and totals are nullish', () => {
expectTypeOf<UserStatsResponse['lists']>().toEqualTypeOf<
number | null | undefined
>();
expectTypeOf<UserStatsResponse['total_minutes']>().toEqualTypeOf<
number | null | undefined
>();
expectTypeOf<UserStatsResponse['total_plays']>().toEqualTypeOf<
number | null | undefined
>();
expectTypeOf<UserStatsResponse['progress']>().toEqualTypeOf<
{ started: number; finished: number; dropped: number } | null | undefined
>();
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ export const userStatsResponseSchema = z.object({
total: z.number().int(),
distribution: distributionResponseSchema,
}),
progress: z.object({
started: z.number().int(),
finished: z.number().int(),
dropped: z.number().int(),
}),
lists: z.number().int(),
total_minutes: z.number().int(),
total_plays: z.number().int(),
progress: z
.object({
started: z.number().int(),
finished: z.number().int(),
dropped: z.number().int(),
})
.nullish(),
lists: z.number().int().nullish(),
total_minutes: z.number().int().nullish(),
total_plays: z.number().int().nullish(),
});
Loading