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
8 changes: 5 additions & 3 deletions src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
MAX_RETRY_COUNT,
MIN_TIMEOUT_MS,
} from './utils/constants';
import { encodePathSegment } from './utils/encodePathSegment';
import { isString } from './utils/isCheckValue';
import { parseQuery } from './utils/parseQuery';
import type { MicroCMSSchemaEntry, TypedMicroCMSClient } from './typedSchema';
Expand Down Expand Up @@ -65,9 +66,10 @@ const createClientImpl = ({
}: MakeRequest) => {
const fetchClient = generateFetchClient(apiKey);
const queryString = parseQuery(queries);
const url = `${baseUrl}/${endpoint}${contentId ? `/${contentId}` : ''}${
queryString ? `?${queryString}` : ''
}`;
const path = [endpoint, ...(contentId ? [contentId] : [])]
.map(encodePathSegment)
.join('/');
const url = `${baseUrl}/${path}${queryString ? `?${queryString}` : ''}`;

const getMessageFromResponse = async (response: Response) => {
// Enclose `response.json()` in a try since it may throw an error
Expand Down
14 changes: 14 additions & 0 deletions src/utils/encodePathSegment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Encode a value so that it occupies exactly one URL path segment.
*
* @param {string} segment
* @return {string} encoded segment
*/
export const encodePathSegment = (segment: string): string => {
const value = String(segment);
// `.` and `..` survive percent-encoding and are resolved as dot segments by the URL parser.
if (value === '' || value === '.' || value === '..') {
throw new Error(`invalid path segment: ${JSON.stringify(value)}`);
}
return encodeURIComponent(value);
};
Loading