Skip to content

Commit 2411ce4

Browse files
feat: related field status query param (#600)
* feat: related field status query param * chore: fix tests
1 parent 611b901 commit 2411ce4

8 files changed

Lines changed: 30 additions & 9 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,13 @@ Plugin supports both **REST API** and **GraphQL API** exposed by Strapi.
363363
> `https://localhost:1337/api/navigation/render/njx99iv4p4txuqp307ye8625?menu=true`
364364
365365
- `path` - String value for querying navigation items by its path:
366+
366367
> `https://localhost:1337/api/navigation/render/njx99iv4p4txuqp307ye8625?path=/home/about-us`
367368
369+
- `status` - Enum value representing the status of the related entity that should be returned. Can be set to `draft` or `published`. The default value is `published`:
370+
371+
> `https://localhost:1337/api/navigation/render/njx99iv4p4txuqp307ye8625?status=draft`
372+
368373
### REST API
369374

370375
`GET <host>/api/navigation/?locale=<locale>&orderBy=<orderBy>&orderDirection=<orderDirection>`

server/src/controllers/client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default function clientController(context: { strapi: Core.Strapi }) {
4343
path: rootPath,
4444
locale,
4545
populate,
46+
status = 'published',
4647
} = renderQuerySchema.parse(query);
4748
const idOrSlug = z.string().parse(params.idOrSlug);
4849

@@ -63,14 +64,15 @@ export default function clientController(context: { strapi: Core.Strapi }) {
6364
: populate
6465
)
6566
),
67+
status
6668
});
6769
},
6870

6971
async renderChild(ctx: KoaContext) {
7072
const { params, query = {} } = ctx;
71-
const { type, menu: menuOnly, locale } = renderChildQueryParams.parse(query);
73+
const { type, menu: menuOnly, locale, status = 'published' } = renderChildQueryParams.parse(query);
7274

73-
const idOrSlug = z.string().parse(params.documentId);
75+
const idOrSlug = z.string().parse(params.idOrSlug);
7476
const childUIKey = z.string().parse(params.childUIKey);
7577

7678
return await this.getService().renderChildren({
@@ -79,6 +81,7 @@ export default function clientController(context: { strapi: Core.Strapi }) {
7981
type,
8082
menuOnly: menuOnly === 'true',
8183
locale,
84+
status
8285
});
8386
},
8487
};

server/src/controllers/validators.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export const readAllQuerySchema = z.object({
1212

1313
export const renderTypeSchema = z.enum(['FLAT', 'TREE', 'RFR']);
1414

15+
export const statusSchema = z.enum(['draft', 'published']);
16+
1517
export const populateSchema = z.union([z.boolean(), z.string(), z.string().array(), z.undefined()]);
1618

1719
export const renderQuerySchema = z.object({
@@ -20,12 +22,14 @@ export const renderQuerySchema = z.object({
2022
path: z.string().optional(),
2123
locale: z.string().optional(),
2224
populate: populateSchema.optional(),
25+
status: statusSchema.optional(),
2326
});
2427

2528
export const renderChildQueryParams = z.object({
2629
type: renderTypeSchema.optional(),
2730
menu: booleanStringSchema.optional(),
2831
locale: z.string().optional(),
32+
status: statusSchema.optional(),
2933
});
3034

3135
export const fillFromOtherLocaleParams = z.object({

server/src/services/client/client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ const clientService = (context: { strapi: Core.Strapi }) => ({
239239
rootPath,
240240
type = 'FLAT',
241241
wrapRelated,
242+
status = 'published',
242243
}: RenderTypeInput) {
243244
const adminService = getPluginService(context, 'admin');
244245
const commonService = getPluginService(context, 'common');
@@ -288,6 +289,7 @@ const clientService = (context: { strapi: Core.Strapi }) => ({
288289
master: navigation,
289290
navigationItems,
290291
populate,
292+
status,
291293
});
292294

293295
const { contentTypes, contentTypesNameFields, additionalFields } = await adminService.config({
@@ -471,6 +473,7 @@ const clientService = (context: { strapi: Core.Strapi }) => ({
471473
menuOnly,
472474
type = 'FLAT',
473475
wrapRelated,
476+
status,
474477
}: RenderChildrenInput) {
475478
const criteria = { $or: [{ documentId: idOrSlug }, { slug: idOrSlug }] };
476479
const filter = type === 'FLAT' ? undefined : childUIKey;
@@ -486,6 +489,7 @@ const clientService = (context: { strapi: Core.Strapi }) => ({
486489
filter,
487490
wrapRelated,
488491
locale,
492+
status,
489493
});
490494
},
491495

@@ -497,6 +501,7 @@ const clientService = (context: { strapi: Core.Strapi }) => ({
497501
rootPath,
498502
type = 'FLAT',
499503
wrapRelated,
504+
status,
500505
}: RenderInput) {
501506
const criteria = { $or: [{ documentId: idOrSlug }, { slug: idOrSlug }] };
502507
const itemCriteria = menuOnly ? { menuAttached: true } : {};
@@ -509,6 +514,7 @@ const clientService = (context: { strapi: Core.Strapi }) => ({
509514
wrapRelated,
510515
locale,
511516
populate,
517+
status,
512518
});
513519
},
514520
});

server/src/services/client/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export interface RenderTypeInput {
5858
wrapRelated?: boolean;
5959
populate?: PopulateQueryParam;
6060
locale?: string;
61+
status?: 'draft' | 'published';
6162
}
6263

6364
export interface RenderChildrenInput {
@@ -67,6 +68,7 @@ export interface RenderChildrenInput {
6768
menuOnly?: boolean;
6869
wrapRelated?: boolean;
6970
locale?: string;
71+
status?: 'draft' | 'published';
7072
}
7173

7274
export interface RenderInput {
@@ -77,4 +79,5 @@ export interface RenderInput {
7779
wrapRelated?: boolean;
7880
populate?: PopulateQueryParam;
7981
locale?: string;
82+
status?: 'draft' | 'published';
8083
}

server/src/services/common/common.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const commonService = (context: { strapi: Core.Strapi }) => ({
6161
navigationItems,
6262
parent,
6363
populate,
64+
status = 'published',
6465
}: MapToNavigationItemDTOInput): Promise<NavigationItemDTO[]> {
6566
const result: NavigationItemDTO[] = [];
6667

@@ -79,15 +80,12 @@ const commonService = (context: { strapi: Core.Strapi }) => ({
7980

8081
const fieldsToPopulate = config.contentTypesPopulate[item.related.__type];
8182

82-
if (!fieldsToPopulate?.length) {
83-
return item;
84-
}
85-
8683
const repository = getGenericRepository({ strapi }, item.related.__type as UID.ContentType);
84+
8785
const related = await repository.findById(
8886
item.related.documentId,
8987
fieldsToPopulate,
90-
'published',
88+
status,
9189
{
9290
locale,
9391
}
@@ -116,6 +114,7 @@ const commonService = (context: { strapi: Core.Strapi }) => ({
116114
master,
117115
parent: base as NavigationItemDTO,
118116
locale,
117+
status,
119118
}),
120119
} as NavigationItemDTO);
121120
}

server/src/services/common/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface MapToNavigationItemDTOInput {
2929
master?: Omit<NavigationDTO, 'items'>;
3030
parent?: NavigationItemDTO;
3131
locale?: string;
32+
status?: 'draft' | 'published';
3233
}
3334

3435
export interface CreateBranchInput {

server/tests/controllers/client.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ describe('Navigation', () => {
203203
const navigationItem = getMockNavigationItem();
204204
const renderChildren = jest.fn();
205205
const mockClientService = asProxy<ClientService>({ renderChildren });
206-
const documentId = faker.string.uuid();
206+
const idOrSlug = faker.string.uuid();
207207
const childUIKey = faker.string.sample(10);
208208
const type = faker.helpers.arrayElement(['FLAT', 'TREE', 'RFR']);
209209
const menu = faker.datatype.boolean().toString();
@@ -217,7 +217,7 @@ describe('Navigation', () => {
217217
// When
218218
const result = await clientController.renderChild(
219219
asProxy<KoaContext>({
220-
params: { documentId, childUIKey },
220+
params: { idOrSlug, childUIKey },
221221
query: {
222222
type,
223223
menu,

0 commit comments

Comments
 (0)