Skip to content

Commit

Permalink
feat(services-bookmark): Update bookmark module schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
odinr committed Sep 4, 2024
1 parent 109e2cf commit 260c2f4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { ClientMethod, ExtractApiVersion, FilterAllowedApiVersions } from '

import { extractVersion, schemaSelector } from '../../utils';
import { ApiVersion } from '../api-version';
import { ApiBookmarkPayload } from '../schemas';

/** API version which this operation uses. */
type AvailableVersions = ApiVersion.v1;
Expand All @@ -27,18 +28,7 @@ const ArgSchema = {
const ApiResponseSchema = {
[ApiVersion.v1]: z.object({
id: z.string(),
payload: z
.string()
.or(z.record(z.unknown()))
.optional()
.default('')
.transform((x) => {
try {
return typeof x === 'string' ? JSON.parse(x) : x;
} catch {
return x;
}
}),
payload: ApiBookmarkPayload[ApiVersion.v1],
}),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { ClientMethod, ExtractApiVersion, FilterAllowedApiVersions } from '

import { extractVersion, schemaSelector } from '../../utils';
import { ApiVersion } from '../api-version';
import { ApiBookmarkSchema } from '../schemas';
import { ApiBookmarkPayload, ApiBookmarkSchema } from '../schemas';

/** API version which this operation uses. */
type AvailableVersions = ApiVersion.v1;
Expand Down Expand Up @@ -45,9 +45,11 @@ const ArgSchema = {

/** Schema for the response from the API. */
const ApiResponseSchema = {
[ApiVersion.v1]: ApiBookmarkSchema[ApiVersion.v1].extend({
payload: z.string().or(z.record(z.unknown())).optional(),
}),
[ApiVersion.v1]: ApiBookmarkSchema[ApiVersion.v1].and(
z.object({
payload: ApiBookmarkPayload[ApiVersion.v1].optional(),
}),
),
};

/** Defines the expected output from the api. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { ClientMethod, ExtractApiVersion, FilterAllowedApiVersions } from '

import { extractVersion, schemaSelector } from '../../utils';
import { ApiVersion } from '../api-version';
import { ApiBookmarkSchema, ApiSourceSystem } from '../schemas';
import { ApiBookmarkPayload, ApiBookmarkSchema, ApiSourceSystem } from '../schemas';

/** API version which this operation uses. */
type AvailableVersions = ApiVersion.v1;
Expand All @@ -24,11 +24,7 @@ const ArgSchema = {
[ApiVersion.v1]: z.object({
name: z.string(),
appKey: z.string(),
payload: z
.record(z.unknown())
.or(z.string())
.optional()
.transform((x) => (typeof x === 'object' ? JSON.stringify(x) : x)),
payload: z.record(z.unknown()).or(z.string()).optional(),
description: z.string().nullish(),
isShared: z.boolean().nullish(),
contextId: z.string().nullish(),
Expand All @@ -38,7 +34,11 @@ const ArgSchema = {

/** Schema for the response from the API. */
const ApiResponseSchema = {
[ApiVersion.v1]: ApiBookmarkSchema[ApiVersion.v1],
[ApiVersion.v1]: ApiBookmarkSchema[ApiVersion.v1].and(
z.object({
payload: ApiBookmarkPayload[ApiVersion.v1].optional(),
}),
),
};

/** Defines the expected output from the api. */
Expand Down
17 changes: 17 additions & 0 deletions packages/modules/services/src/bookmarks/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ export const ApiBookmarkSchema = {
return this[ApiVersion.v1];
},
};

export const ApiBookmarkPayload = {
get [ApiVersion.v1]() {
return z
.record(z.unknown())
.or(z.string())
.optional()
.default('')
.transform((x) => {
try {
return typeof x === 'string' ? JSON.parse(x) : x;
} catch {
return x;
}
});
},
};

0 comments on commit 260c2f4

Please sign in to comment.