Skip to content
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ type useMicroCMSGetObject = <T>(
// https://nuxt.com/docs/api/composables/use-fetch
```

`endpoint` and `contentId` must consist only of letters, digits, `-` and `_` (the characters microCMS allows for them). Any other value, such as one containing `/`, `\`, `.` or `?`, throws an error and no request is sent.

```vue
<template>
<ul>
Expand Down
27 changes: 22 additions & 5 deletions src/runtime/composables/useMicroCMSGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { computed } from 'vue';
import { useMicroCMSUrl } from './useMicroCMSUrl';

type MicroCMSGetArgs = {
url: string;
segments: string[];
queries?: MicroCMSQueries;
};

Expand All @@ -38,10 +38,27 @@ type FetchOptions<T = void> = Omit<

const method = 'get';

// microCMS endpoints and content IDs only allow these characters, so anything
// else (slashes, backslashes, dots, `?`, `#`, `%`) cannot address the intended resource.
const PATH_SEGMENT = /^[A-Za-z0-9_-]+$/;

const toPath = (segments: unknown[]) =>
segments
.map((segment) => {
if (typeof segment !== 'string' || !PATH_SEGMENT.test(segment)) {
throw new Error(
'Invalid microCMS endpoint or contentId: only letters, digits, "-" and "_" are allowed'
);
}
return encodeURIComponent(segment);
})
.join('/');

const useMicroCMSGet = <T>(
{ url, queries }: MicroCMSGetArgs,
{ segments, queries }: MicroCMSGetArgs,
fetchOptions: FetchOptions<T> = {}
) => {
const url = toPath(segments);
const baseURL = useMicroCMSUrl();
const config = useRuntimeConfig();

Expand Down Expand Up @@ -74,7 +91,7 @@ export const useMicroCMSGetList = <T>(
fetchOptions: FetchOptions<MicroCMSListResponse<T>> = {}
) => {
return useMicroCMSGet<MicroCMSListResponse<T>>(
{ url: endpoint, queries },
{ segments: [endpoint], queries },
fetchOptions
);
};
Expand All @@ -84,7 +101,7 @@ export const useMicroCMSGetListDetail = <T>(
fetchOptions: FetchOptions<T & MicroCMSListContent> = {}
) => {
return useMicroCMSGet<T & MicroCMSListContent>(
{ url: `${endpoint}/${contentId}`, queries },
{ segments: [endpoint, contentId], queries },
fetchOptions
);
};
Expand All @@ -94,7 +111,7 @@ export const useMicroCMSGetObject = <T>(
fetchOptions: FetchOptions<T & MicroCMSObjectContent> = {}
) => {
return useMicroCMSGet<T & MicroCMSObjectContent>(
{ url: endpoint, queries },
{ segments: [endpoint], queries },
fetchOptions
);
};