create-packer 1.26.2 → 1.26.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/createTemp.js +1 -1
- package/package.json +1 -1
- package/template/cli/tsconfig.json +1 -0
- package/template/lib/react/tsconfig.json +1 -0
- package/template/lib/ts/tsconfig.json +1 -0
- package/template/web-app/next/tsconfig.json +19 -26
- package/template/web-app/react/tsconfig.json +3 -3
- package/template/web-app/react-webpack/tsconfig.json +2 -1
- package/template/web-app/svelte/tsconfig.json +1 -0
- package/template/web-extension/shared/service/tools/base.ts +33 -0
- package/template/web-extension/shared/service/tools/createRequestActions.ts +58 -0
- package/template/web-extension/shared/service/tools/createService.ts +46 -0
- package/template/web-extension/shared/service/tools/createServiceHooks.ts +65 -0
- package/template/web-extension/shared/service/tools/index.ts +4 -0
- package/template/web-extension/shared/service/types.ts +29 -11
- package/template/web-extension/tsconfig.json +4 -4
- package/template/workspace/pnpm/packages/test/tsconfig.json +1 -0
- package/template/workspace/pnpm/tsconfig.json +16 -15
- package/template/web-extension/shared/service/tools.ts +0 -132
package/bin/createTemp.js
CHANGED
|
@@ -38,7 +38,7 @@ async function createTemp(dirname) {
|
|
|
38
38
|
}
|
|
39
39
|
]);
|
|
40
40
|
let tempInfo = tempInfoList.find(o => o.name === answer.temp);
|
|
41
|
-
if (tempInfo.children.length > 0) {
|
|
41
|
+
if (tempInfo?.children && tempInfo.children.length > 0) {
|
|
42
42
|
answer = await inquirer.prompt([
|
|
43
43
|
{
|
|
44
44
|
type: 'list',
|
package/package.json
CHANGED
|
@@ -1,28 +1,21 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
],
|
|
22
|
-
"paths": {
|
|
23
|
-
"@/*": ["./*"]
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
|
27
|
-
"exclude": ["node_modules"]
|
|
2
|
+
"extends": "./.svelte-kit/tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"allowJs": true,
|
|
5
|
+
"strictNullChecks": true,
|
|
6
|
+
"checkJs": true,
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"forceConsistentCasingInFileNames": true,
|
|
9
|
+
"resolveJsonModule": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"sourceMap": true,
|
|
12
|
+
"strict": true,
|
|
13
|
+
"paths": {
|
|
14
|
+
"@/*": ["./*"]
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
|
|
18
|
+
//
|
|
19
|
+
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
|
|
20
|
+
// from the referenced tsconfig.json - TypeScript does not merge them in
|
|
28
21
|
}
|
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
"target": "ESNext",
|
|
5
5
|
"useDefineForClassFields": true,
|
|
6
6
|
"lib": ["DOM", "DOM.Iterable", "ESNext"],
|
|
7
|
-
"allowJs":
|
|
7
|
+
"allowJs": true,
|
|
8
|
+
"strictNullChecks": true,
|
|
8
9
|
"skipLibCheck": true,
|
|
9
10
|
"esModuleInterop": false,
|
|
10
11
|
"allowSyntheticDefaultImports": true,
|
|
@@ -22,6 +23,5 @@
|
|
|
22
23
|
}
|
|
23
24
|
},
|
|
24
25
|
"include": ["**/*.tsx", "**/*.ts"],
|
|
25
|
-
"exclude": ["scripts"
|
|
26
|
-
"references": [{ "path": "./tsconfig.node.json" }]
|
|
26
|
+
"exclude": ["scripts"]
|
|
27
27
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { httpBodyType } from '../types'
|
|
2
|
+
|
|
3
|
+
export class FetchError extends Error {
|
|
4
|
+
public request: Request
|
|
5
|
+
|
|
6
|
+
constructor(request: Request) {
|
|
7
|
+
super()
|
|
8
|
+
this.name = 'FetchError'
|
|
9
|
+
this.request = request
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function pickRequestBody<Body = httpBodyType>(
|
|
14
|
+
req: Request
|
|
15
|
+
): Promise<Body | undefined> {
|
|
16
|
+
let reqBody = void 0
|
|
17
|
+
try {
|
|
18
|
+
reqBody = await req.clone().json()
|
|
19
|
+
} catch {
|
|
20
|
+
/* empty */
|
|
21
|
+
}
|
|
22
|
+
return reqBody
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function pickReponseBody<Body = any>(res: Response): Promise<Body | undefined> {
|
|
26
|
+
let body = void 0
|
|
27
|
+
try {
|
|
28
|
+
body = await res.clone().json()
|
|
29
|
+
} catch {
|
|
30
|
+
/** empty */
|
|
31
|
+
}
|
|
32
|
+
return body
|
|
33
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { KyInstance, Options } from 'ky'
|
|
2
|
+
import { Input } from 'ky/distribution/types/options'
|
|
3
|
+
import { includes, isString, omit } from 'lodash-es'
|
|
4
|
+
import { stringify } from 'qs'
|
|
5
|
+
import { Nullable } from '1k-types'
|
|
6
|
+
import { configType, httpErrorType, requestOptionsType, serviceHooksType } from '../types'
|
|
7
|
+
import { FetchError } from './base'
|
|
8
|
+
|
|
9
|
+
export function createRequestActions(
|
|
10
|
+
prefixUrl: configType['prefixUrl'],
|
|
11
|
+
request: KyInstance,
|
|
12
|
+
hooks: serviceHooksType
|
|
13
|
+
) {
|
|
14
|
+
function creator(method: Required<Options>['method']) {
|
|
15
|
+
return async function <DATA = any>(url: Input, option?: requestOptionsType): Promise<DATA> {
|
|
16
|
+
try {
|
|
17
|
+
return await request[method](url, omit(option, 'responseType'))[
|
|
18
|
+
option?.responseType || 'json'
|
|
19
|
+
]()
|
|
20
|
+
} catch (e: any) {
|
|
21
|
+
const searchParamsString = stringify(option?.searchParams, { addQueryPrefix: true })
|
|
22
|
+
let httpBody: Nullable<string> = void 0
|
|
23
|
+
if (option && isString(option?.body)) {
|
|
24
|
+
httpBody = option.body
|
|
25
|
+
} else if (option?.body) {
|
|
26
|
+
httpBody = 'other'
|
|
27
|
+
} else if (option?.json) {
|
|
28
|
+
httpBody = JSON.stringify(option.json)
|
|
29
|
+
}
|
|
30
|
+
let error = new FetchError(
|
|
31
|
+
new Request(prefixUrl + '/' + url + searchParamsString, {
|
|
32
|
+
method,
|
|
33
|
+
body: httpBody
|
|
34
|
+
})
|
|
35
|
+
)
|
|
36
|
+
error.message = e.message
|
|
37
|
+
if (includes([httpErrorType.HTTPError, httpErrorType.TimeoutError], e.name)) {
|
|
38
|
+
error = e
|
|
39
|
+
}
|
|
40
|
+
for (let i = 0; i < hooks.httpError.length; i++) {
|
|
41
|
+
await hooks.httpError[i](error)
|
|
42
|
+
}
|
|
43
|
+
throw error
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
get: creator('get'),
|
|
50
|
+
post: creator('post'),
|
|
51
|
+
put: creator('put'),
|
|
52
|
+
patch: creator('patch'),
|
|
53
|
+
head: creator('head'),
|
|
54
|
+
delete: creator('delete')
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type requestActionsType = ReturnType<typeof createRequestActions>
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import ky from 'ky'
|
|
2
|
+
import { assign, isArray, isObject } from 'lodash-es'
|
|
3
|
+
import { configType } from '../types'
|
|
4
|
+
import { createRequestActions } from './createRequestActions'
|
|
5
|
+
import { createServiceHooks } from './createServiceHooks'
|
|
6
|
+
|
|
7
|
+
export function createService(config: configType) {
|
|
8
|
+
const { kyHooks, addHooks, serviceHooks } = createServiceHooks()
|
|
9
|
+
const globalParams: configType['globalParams'] = assign({}, config.globalParams)
|
|
10
|
+
const globalSearchParams: configType['globalSearchParams'] = assign(
|
|
11
|
+
{},
|
|
12
|
+
config.globalSearchParams
|
|
13
|
+
)
|
|
14
|
+
const request = ky.create({
|
|
15
|
+
prefixUrl: config.prefixUrl,
|
|
16
|
+
headers: config.headers,
|
|
17
|
+
hooks: kyHooks
|
|
18
|
+
})
|
|
19
|
+
const requestActions = createRequestActions(config.prefixUrl, request, serviceHooks)
|
|
20
|
+
|
|
21
|
+
function setGlobalParams(params: configType['globalParams']) {
|
|
22
|
+
assign(globalParams, params)
|
|
23
|
+
}
|
|
24
|
+
function setGlobalSearchParams(params: configType['globalSearchParams']) {
|
|
25
|
+
assign(globalSearchParams, params)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// init
|
|
29
|
+
// ------------------------------------------------------------------------
|
|
30
|
+
addHooks('beforeRequest', async req => {
|
|
31
|
+
const { searchParams, body } = req
|
|
32
|
+
req.searchParams = { ...globalSearchParams, ...searchParams }
|
|
33
|
+
|
|
34
|
+
if (!isArray(body) && isObject(body)) {
|
|
35
|
+
req.body = { ...globalParams, ...body }
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
instance: request,
|
|
41
|
+
...requestActions,
|
|
42
|
+
addHooks,
|
|
43
|
+
setGlobalParams,
|
|
44
|
+
setGlobalSearchParams
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Hooks } from 'ky'
|
|
2
|
+
import { omit } from 'lodash-es'
|
|
3
|
+
import { parse, stringify } from 'qs'
|
|
4
|
+
import { ArrayValues } from '1k-types'
|
|
5
|
+
import { serviceHooksType } from '../types'
|
|
6
|
+
import { pickRequestBody } from './base'
|
|
7
|
+
|
|
8
|
+
export function createServiceHooks(hooks?: Partial<serviceHooksType>) {
|
|
9
|
+
const serviceHooks: serviceHooksType = {
|
|
10
|
+
httpError: [],
|
|
11
|
+
beforeRequest: [],
|
|
12
|
+
afterResponse: [],
|
|
13
|
+
...(hooks || {})
|
|
14
|
+
}
|
|
15
|
+
const kyHooks: Hooks = {
|
|
16
|
+
beforeRequest: [
|
|
17
|
+
async req => {
|
|
18
|
+
// eslint-disable-next-line prefer-const
|
|
19
|
+
let [url, searchParams] = req.url.split('?')
|
|
20
|
+
const reqBody = await pickRequestBody(req)
|
|
21
|
+
const parsedSearchParams = parse(searchParams)
|
|
22
|
+
const reqConfig = {
|
|
23
|
+
searchParams: parsedSearchParams,
|
|
24
|
+
body: reqBody,
|
|
25
|
+
headers: req.headers,
|
|
26
|
+
method: req.method
|
|
27
|
+
}
|
|
28
|
+
await foreachHooks('beforeRequest', reqConfig)
|
|
29
|
+
url = url + stringify(reqConfig.searchParams, { addQueryPrefix: true })
|
|
30
|
+
return new Request(url, {
|
|
31
|
+
...omit(req, 'url'),
|
|
32
|
+
headers: reqConfig.headers,
|
|
33
|
+
body: reqBody ? JSON.stringify(reqConfig.body) : void 0
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
afterResponse: [
|
|
38
|
+
async (request, options, response) => {
|
|
39
|
+
await foreachHooks('afterResponse', { request, options, response })
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
function addHooks<K extends keyof serviceHooksType>(
|
|
44
|
+
key: K,
|
|
45
|
+
callback: ArrayValues<serviceHooksType[K]>
|
|
46
|
+
) {
|
|
47
|
+
serviceHooks[key].push(callback as any)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function foreachHooks<K extends keyof serviceHooksType>(
|
|
51
|
+
name: K,
|
|
52
|
+
arg: Parameters<ArrayValues<serviceHooksType[K]>>[0]
|
|
53
|
+
) {
|
|
54
|
+
for (let i = 0; i < serviceHooks[name].length; i++) {
|
|
55
|
+
const fn = serviceHooks[name][i]
|
|
56
|
+
await fn(arg as never)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
kyHooks,
|
|
62
|
+
serviceHooks,
|
|
63
|
+
addHooks
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -1,22 +1,40 @@
|
|
|
1
|
-
import { Options,
|
|
2
|
-
import { Nullable } from '1k-types'
|
|
1
|
+
import { HTTPError, NormalizedOptions, Options, TimeoutError } from 'ky'
|
|
2
|
+
import { AnyArray, AnyObject, Nullable } from '1k-types'
|
|
3
|
+
import { FetchError } from './tools'
|
|
3
4
|
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export interface requestOptionsType extends Options {
|
|
9
|
-
responseType?: 'json' | 'text' | 'blob'
|
|
5
|
+
export enum httpErrorType {
|
|
6
|
+
'HTTPError' = 'HTTPError',
|
|
7
|
+
'TimeoutError' = 'TimeoutError',
|
|
8
|
+
'FetchError' = 'FetchError'
|
|
10
9
|
}
|
|
11
10
|
|
|
11
|
+
export type httpBodyType = Nullable<string | number | AnyObject | AnyArray>
|
|
12
|
+
|
|
12
13
|
export type beforeRequestType = (req: {
|
|
13
14
|
searchParams: Record<string, any>
|
|
14
|
-
body:
|
|
15
|
+
body: httpBodyType
|
|
15
16
|
headers: Headers
|
|
16
17
|
method: Options['method']
|
|
17
18
|
}) => Promise<void> | void
|
|
18
|
-
export type
|
|
19
|
+
export type httpErrorHooksType = (
|
|
20
|
+
error: HTTPError | TimeoutError | FetchError
|
|
21
|
+
) => Promise<void> | void
|
|
22
|
+
export type AfterResponseHook = (arg: {
|
|
23
|
+
request: Request
|
|
24
|
+
options: NormalizedOptions
|
|
25
|
+
response: Response
|
|
26
|
+
}) => Promise<void> | void
|
|
19
27
|
export interface serviceHooksType {
|
|
20
|
-
|
|
28
|
+
httpError: httpErrorHooksType[]
|
|
21
29
|
beforeRequest: beforeRequestType[]
|
|
30
|
+
afterResponse: AfterResponseHook[]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface configType extends Pick<Options, 'prefixUrl' | 'headers'> {
|
|
34
|
+
globalParams?: Record<string, any>
|
|
35
|
+
globalSearchParams?: Record<string, any>
|
|
36
|
+
hooks?: Partial<serviceHooksType>
|
|
37
|
+
}
|
|
38
|
+
export interface requestOptionsType extends Options {
|
|
39
|
+
responseType?: 'json' | 'text' | 'blob'
|
|
22
40
|
}
|
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
"target": "ESNext",
|
|
5
5
|
"useDefineForClassFields": true,
|
|
6
6
|
"lib": ["DOM", "DOM.Iterable", "ESNext"],
|
|
7
|
-
"allowJs":
|
|
7
|
+
"allowJs": true,
|
|
8
|
+
"strictNullChecks": true,
|
|
8
9
|
"skipLibCheck": true,
|
|
9
10
|
"esModuleInterop": false,
|
|
10
11
|
"allowSyntheticDefaultImports": true,
|
|
12
|
+
"noImplicitAny": false,
|
|
11
13
|
"strict": true,
|
|
12
14
|
"forceConsistentCasingInFileNames": true,
|
|
13
15
|
"strictPropertyInitialization": false,
|
|
@@ -15,7 +17,6 @@
|
|
|
15
17
|
"moduleResolution": "Node",
|
|
16
18
|
"resolveJsonModule": true,
|
|
17
19
|
"isolatedModules": true,
|
|
18
|
-
"noImplicitAny": false,
|
|
19
20
|
"noEmit": true,
|
|
20
21
|
"jsx": "react-jsx",
|
|
21
22
|
"paths": {
|
|
@@ -23,6 +24,5 @@
|
|
|
23
24
|
}
|
|
24
25
|
},
|
|
25
26
|
"include": ["**/*.tsx", "**/*.ts"],
|
|
26
|
-
"exclude": ["scripts"
|
|
27
|
-
"references": [{ "path": "./tsconfig.node.json" }]
|
|
27
|
+
"exclude": ["scripts"]
|
|
28
28
|
}
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "CommonJS",
|
|
4
|
+
"esModuleInterop": true,
|
|
5
|
+
"allowSyntheticDefaultImports": true,
|
|
6
|
+
"allowJs": true,
|
|
7
|
+
"strictNullChecks": true,
|
|
8
|
+
"target": "ESNext",
|
|
9
|
+
"noImplicitAny": false,
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"sourceMap": false,
|
|
12
|
+
"declaration": true,
|
|
13
|
+
"outDir": "lib",
|
|
14
|
+
"baseUrl": "."
|
|
15
|
+
},
|
|
16
|
+
"include": ["tasks/**/*"],
|
|
17
|
+
"exclude": ["node_modules/**/*"]
|
|
17
18
|
}
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import ky, { Hooks, KyInstance, Options } from 'ky'
|
|
2
|
-
import { Input } from 'ky/distribution/types/options'
|
|
3
|
-
import { assign, includes, isArray, isObject, omit } from 'lodash-es'
|
|
4
|
-
import { parse, stringify } from 'qs'
|
|
5
|
-
import { ArrayValues } from '1k-types'
|
|
6
|
-
import { configType, requestOptionsType, serviceHooksType } from './types'
|
|
7
|
-
|
|
8
|
-
function createServiceHooks() {
|
|
9
|
-
const serviceHooks: serviceHooksType = {
|
|
10
|
-
beforeError: [],
|
|
11
|
-
beforeRequest: []
|
|
12
|
-
}
|
|
13
|
-
const kyHooks: Hooks = {
|
|
14
|
-
beforeError: [
|
|
15
|
-
async error => {
|
|
16
|
-
await foreachHooks('beforeError', error)
|
|
17
|
-
return error
|
|
18
|
-
}
|
|
19
|
-
],
|
|
20
|
-
beforeRequest: [
|
|
21
|
-
async req => {
|
|
22
|
-
// eslint-disable-next-line prefer-const
|
|
23
|
-
let [url, searchParams] = req.url.split('?')
|
|
24
|
-
let reqBody = void 0
|
|
25
|
-
if (!includes(['GET', 'HEAD'], req.method)) {
|
|
26
|
-
try {
|
|
27
|
-
reqBody = await req.json()
|
|
28
|
-
} catch {
|
|
29
|
-
/* empty */
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
const parsedSearchParams = parse(searchParams)
|
|
33
|
-
const reqConfig = {
|
|
34
|
-
searchParams: parsedSearchParams,
|
|
35
|
-
body: reqBody,
|
|
36
|
-
headers: req.headers,
|
|
37
|
-
method: req.method
|
|
38
|
-
}
|
|
39
|
-
await foreachHooks('beforeRequest', reqConfig)
|
|
40
|
-
url = url + stringify(reqConfig.searchParams, { addQueryPrefix: true })
|
|
41
|
-
return new Request(url, {
|
|
42
|
-
...omit(req, 'url'),
|
|
43
|
-
headers: reqConfig.headers,
|
|
44
|
-
body: reqBody ? JSON.stringify(reqConfig.body) : void 0
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
]
|
|
48
|
-
}
|
|
49
|
-
function addHooks<K extends keyof serviceHooksType>(
|
|
50
|
-
key: K,
|
|
51
|
-
callback: ArrayValues<serviceHooksType[K]>
|
|
52
|
-
) {
|
|
53
|
-
serviceHooks[key].push(callback as any)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async function foreachHooks<K extends keyof serviceHooksType>(
|
|
57
|
-
name: K,
|
|
58
|
-
arg: Parameters<ArrayValues<serviceHooksType[K]>>[0]
|
|
59
|
-
) {
|
|
60
|
-
for (let i = 0; i < serviceHooks[name].length; i++) {
|
|
61
|
-
const fn = serviceHooks[name][i]
|
|
62
|
-
await fn(arg as never)
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return {
|
|
67
|
-
kyHooks,
|
|
68
|
-
serviceHooks,
|
|
69
|
-
addHooks
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function createRequestActions(request: KyInstance) {
|
|
74
|
-
function creator(method: Required<Options>['method']) {
|
|
75
|
-
return function <DATA = any>(url: Input, option?: requestOptionsType): Promise<DATA> {
|
|
76
|
-
return request[method](url, omit(option, 'responseType'))[
|
|
77
|
-
option?.responseType || 'json'
|
|
78
|
-
]()
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
get: creator('get'),
|
|
84
|
-
post: creator('post'),
|
|
85
|
-
put: creator('put'),
|
|
86
|
-
patch: creator('patch'),
|
|
87
|
-
head: creator('head'),
|
|
88
|
-
delete: creator('delete')
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
export type requestActionsType = ReturnType<typeof createRequestActions>
|
|
92
|
-
|
|
93
|
-
export function createService(config: configType) {
|
|
94
|
-
const { kyHooks, addHooks } = createServiceHooks()
|
|
95
|
-
const globalParams: configType['globalParams'] = assign({}, config.globalParams)
|
|
96
|
-
const globalSearchParams: configType['globalSearchParams'] = assign(
|
|
97
|
-
{},
|
|
98
|
-
config.globalSearchParams
|
|
99
|
-
)
|
|
100
|
-
const request = ky.create({
|
|
101
|
-
prefixUrl: config.prefixUrl,
|
|
102
|
-
headers: config.headers,
|
|
103
|
-
hooks: kyHooks
|
|
104
|
-
})
|
|
105
|
-
const requestActions = createRequestActions(request)
|
|
106
|
-
|
|
107
|
-
function setGlobalParams(params: configType['globalParams']) {
|
|
108
|
-
assign(globalParams, params)
|
|
109
|
-
}
|
|
110
|
-
function setGlobalSearchParams(params: configType['globalSearchParams']) {
|
|
111
|
-
assign(globalSearchParams, params)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// 初始化
|
|
115
|
-
// ------------------------------------------------------------------------
|
|
116
|
-
addHooks('beforeRequest', async req => {
|
|
117
|
-
const { searchParams, body } = req
|
|
118
|
-
req.searchParams = { ...globalSearchParams, ...searchParams }
|
|
119
|
-
|
|
120
|
-
if (!isArray(body) && isObject(body)) {
|
|
121
|
-
req.body = { ...globalParams, ...body }
|
|
122
|
-
}
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
return {
|
|
126
|
-
instance: request,
|
|
127
|
-
...requestActions,
|
|
128
|
-
addHooks,
|
|
129
|
-
setGlobalParams,
|
|
130
|
-
setGlobalSearchParams
|
|
131
|
-
}
|
|
132
|
-
}
|