From 29b0f677518159d16762d0695f8f235b3b2d7f5b Mon Sep 17 00:00:00 2001 From: totto2727 Date: Sun, 9 Jul 2023 18:55:43 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20createClient=E9=96=A2=E6=95=B0=E3=81=A7?= =?UTF-8?q?=E7=94=9F=E6=88=90=E3=81=95=E3=82=8C=E3=82=8B=E9=96=A2=E6=95=B0?= =?UTF-8?q?=E3=81=AE=E5=BC=95=E6=95=B0=E3=81=ABreadonly=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 21 +++++++++++++++++---- package.json | 4 +++- src/createClient.ts | 21 +++++++++++---------- src/utils/parseQuery.ts | 3 ++- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index a2b9389..541b0d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,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", @@ -9261,6 +9262,14 @@ "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, + "node_modules/ts-essentials": { + "version": "9.3.2", + "resolved": "https://registry.npmjs.org/ts-essentials/-/ts-essentials-9.3.2.tgz", + "integrity": "sha512-JxKJzuWqH1MmH4ZFHtJzGEhkfN3QvVR3C3w+4BIoWeoY68UVVoA2Np/Bca9z0IPSErVCWhv439aT0We4Dks8kQ==", + "peerDependencies": { + "typescript": ">=4.1.0" + } + }, "node_modules/ts-jest": { "version": "28.0.7", "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-28.0.7.tgz", @@ -9362,7 +9371,6 @@ "version": "4.7.4", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.4.tgz", "integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==", - "dev": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -16742,6 +16750,12 @@ "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, + "ts-essentials": { + "version": "9.3.2", + "resolved": "https://registry.npmjs.org/ts-essentials/-/ts-essentials-9.3.2.tgz", + "integrity": "sha512-JxKJzuWqH1MmH4ZFHtJzGEhkfN3QvVR3C3w+4BIoWeoY68UVVoA2Np/Bca9z0IPSErVCWhv439aT0We4Dks8kQ==", + "requires": {} + }, "ts-jest": { "version": "28.0.7", "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-28.0.7.tgz", @@ -16799,8 +16813,7 @@ "typescript": { "version": "4.7.4", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.4.tgz", - "integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==", - "dev": true + "integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==" }, "universalify": { "version": "2.0.0", diff --git a/package.json b/package.json index 3af800a..e835023 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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", diff --git a/src/createClient.ts b/src/createClient.ts index 405d9b6..019b64f 100644 --- a/src/createClient.ts +++ b/src/createClient.ts @@ -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 @@ -36,7 +37,7 @@ export const createClient = ({ apiKey, customFetch, retry: retryOption, -}: MicroCMSClient) => { +}: DeepReadonly) => { if (!serviceDomain || !apiKey) { throw new Error('parameter is required (check serviceDomain and apiKey)'); } @@ -58,7 +59,7 @@ export const createClient = ({ contentId, queries = {}, requestInit, - }: MakeRequest) => { + }: DeepReadonly) => { const fetchClient = generateFetchClient(apiKey, customFetch); const queryString = parseQuery(queries); const url = `${baseUrl}/${endpoint}${contentId ? `/${contentId}` : ''}${ @@ -80,7 +81,7 @@ export const createClient = ({ async (bail) => { try { const response = await fetchClient(url, { - ...requestInit, + ...(requestInit as RequestInit), method: requestInit?.method ?? 'GET', }); @@ -150,7 +151,7 @@ export const createClient = ({ contentId, queries = {}, customRequestInit, - }: GetRequest): Promise => { + }: DeepReadonly): Promise => { if (!endpoint) { return Promise.reject(new Error('endpoint is required')); } @@ -169,7 +170,7 @@ export const createClient = ({ endpoint, queries = {}, customRequestInit, - }: GetListRequest): Promise> => { + }: DeepReadonly): Promise> => { if (!endpoint) { return Promise.reject(new Error('endpoint is required')); } @@ -188,7 +189,7 @@ export const createClient = ({ contentId, queries = {}, customRequestInit, - }: GetListDetailRequest): Promise => { + }: DeepReadonly): Promise => { if (!endpoint) { return Promise.reject(new Error('endpoint is required')); } @@ -207,7 +208,7 @@ export const createClient = ({ endpoint, queries = {}, customRequestInit, - }: GetObjectRequest): Promise => { + }: DeepReadonly): Promise => { if (!endpoint) { return Promise.reject(new Error('endpoint is required')); } @@ -227,7 +228,7 @@ export const createClient = ({ content, isDraft = false, customRequestInit, - }: CreateRequest): Promise => { + }: DeepReadonly>): Promise => { if (!endpoint) { return Promise.reject(new Error('endpoint is required')); } @@ -258,7 +259,7 @@ export const createClient = ({ contentId, content, customRequestInit, - }: UpdateRequest): Promise => { + }: DeepReadonly>): Promise => { if (!endpoint) { return Promise.reject(new Error('endpoint is required')); } @@ -286,7 +287,7 @@ export const createClient = ({ endpoint, contentId, customRequestInit, - }: DeleteRequest): Promise => { + }: DeepReadonly): Promise => { if (!endpoint) { return Promise.reject(new Error('endpoint is required')); } diff --git a/src/utils/parseQuery.ts b/src/utils/parseQuery.ts index edbdeaf..7d4550d 100644 --- a/src/utils/parseQuery.ts +++ b/src/utils/parseQuery.ts @@ -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): string => { if (!isObject(queries)) { throw new Error('queries is not object'); }