Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: createClient関数で生成される関数の引数にreadonlyを追加 #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 17 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"build": "rollup -c",
"lint": "eslint ./src",
"lint:fix": "eslint --fix ./src",
"typecheck": "tsc --noEmit",
"test": "jest --coverage=false",
"test:coverage": "jest --coverage=true"
},
Expand All @@ -32,7 +33,8 @@
"async-retry": "^1.3.3",
"cross-fetch": "^3.1.5",
"encoding": "^0.1.13",
"qs": "^6.10.1"
"qs": "^6.10.1",
"ts-essentials": "^9.3.2"
},
"devDependencies": {
"@rollup/plugin-babel": "^5.3.0",
Expand Down
21 changes: 11 additions & 10 deletions src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from './utils/constants';
import { generateFetchClient } from './lib/fetch';
import retry from 'async-retry';
import { DeepOmit, DeepReadonly } from 'ts-essentials';

/**
* Initialize SDK Client
Expand All @@ -36,7 +37,7 @@ export const createClient = ({
apiKey,
customFetch,
retry: retryOption,
}: MicroCMSClient) => {
}: DeepReadonly<MicroCMSClient>) => {
if (!serviceDomain || !apiKey) {
throw new Error('parameter is required (check serviceDomain and apiKey)');
}
Expand All @@ -58,7 +59,7 @@ export const createClient = ({
contentId,
queries = {},
requestInit,
}: MakeRequest) => {
}: DeepReadonly<MakeRequest>) => {
const fetchClient = generateFetchClient(apiKey, customFetch);
const queryString = parseQuery(queries);
const url = `${baseUrl}/${endpoint}${contentId ? `/${contentId}` : ''}${
Expand All @@ -80,7 +81,7 @@ export const createClient = ({
async (bail) => {
try {
const response = await fetchClient(url, {
...requestInit,
...(requestInit as RequestInit),
method: requestInit?.method ?? 'GET',
});

Expand Down Expand Up @@ -150,7 +151,7 @@ export const createClient = ({
contentId,
queries = {},
customRequestInit,
}: GetRequest): Promise<T> => {
}: DeepReadonly<GetRequest>): Promise<T> => {
if (!endpoint) {
return Promise.reject(new Error('endpoint is required'));
}
Expand All @@ -169,7 +170,7 @@ export const createClient = ({
endpoint,
queries = {},
customRequestInit,
}: GetListRequest): Promise<MicroCMSListResponse<T>> => {
}: DeepReadonly<GetListRequest>): Promise<MicroCMSListResponse<T>> => {
if (!endpoint) {
return Promise.reject(new Error('endpoint is required'));
}
Expand All @@ -188,7 +189,7 @@ export const createClient = ({
contentId,
queries = {},
customRequestInit,
}: GetListDetailRequest): Promise<T & MicroCMSListContent> => {
}: DeepReadonly<GetListDetailRequest>): Promise<T & MicroCMSListContent> => {
if (!endpoint) {
return Promise.reject(new Error('endpoint is required'));
}
Expand All @@ -207,7 +208,7 @@ export const createClient = ({
endpoint,
queries = {},
customRequestInit,
}: GetObjectRequest): Promise<T & MicroCMSObjectContent> => {
}: DeepReadonly<GetObjectRequest>): Promise<T & MicroCMSObjectContent> => {
if (!endpoint) {
return Promise.reject(new Error('endpoint is required'));
}
Expand All @@ -227,7 +228,7 @@ export const createClient = ({
content,
isDraft = false,
customRequestInit,
}: CreateRequest<T>): Promise<WriteApiRequestResult> => {
}: DeepReadonly<CreateRequest<T>>): Promise<WriteApiRequestResult> => {
if (!endpoint) {
return Promise.reject(new Error('endpoint is required'));
}
Expand Down Expand Up @@ -258,7 +259,7 @@ export const createClient = ({
contentId,
content,
customRequestInit,
}: UpdateRequest<T>): Promise<WriteApiRequestResult> => {
}: DeepReadonly<UpdateRequest<T>>): Promise<WriteApiRequestResult> => {
if (!endpoint) {
return Promise.reject(new Error('endpoint is required'));
}
Expand Down Expand Up @@ -286,7 +287,7 @@ export const createClient = ({
endpoint,
contentId,
customRequestInit,
}: DeleteRequest): Promise<void> => {
}: DeepReadonly<DeleteRequest>): Promise<void> => {
if (!endpoint) {
return Promise.reject(new Error('endpoint is required'));
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/parseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import qs from 'qs';
import { isObject } from './isCheckValue';
import { MicroCMSQueries } from '../types';
import { DeepReadonly } from 'ts-essentials';

export const parseQuery = (queries: MicroCMSQueries): string => {
export const parseQuery = (queries: DeepReadonly<MicroCMSQueries>): string => {
if (!isObject(queries)) {
throw new Error('queries is not object');
}
Expand Down