This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
generated from innei-template/rollup-typescript-lib
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import '../helpers/global-fetch' | ||
|
||
import { defaultKyAdaptor } from '~/adaptors/ky' | ||
import { createDefaultKyAdaptor } from '~/adaptors/ky' | ||
|
||
import { testAdaptor } from '../helpers/adaptor-test' | ||
|
||
describe('test ky adaptor', () => { | ||
const defaultKyAdaptor = createDefaultKyAdaptor() | ||
testAdaptor(defaultKyAdaptor) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { Options, ResponsePromise } from 'ky' | ||
import ky from 'ky-universal' | ||
import type { KyInstance } from 'ky/distribution/types/ky' | ||
|
||
import type { IRequestAdapter } from '~/interfaces/adapter' | ||
|
||
// TODO post data not only json, | ||
const getDataFromKyResponse = async (response: ResponsePromise) => { | ||
const res = await response | ||
|
||
const isJsonType = res.headers | ||
.get('content-type') | ||
?.includes('application/json') | ||
const json = isJsonType ? await res.clone().json() : null | ||
|
||
const result: Awaited<ResponsePromise> & { | ||
data: any | ||
} = { | ||
...res, | ||
data: json ?? (await res.clone().text()), | ||
} | ||
return result | ||
} | ||
|
||
export const createKyAdaptor = (ky: KyInstance) => { | ||
const adaptor: IRequestAdapter<KyInstance, ResponsePromise> = | ||
Object.preventExtensions({ | ||
get default() { | ||
return ky | ||
}, | ||
|
||
responseWrapper: {} as any as ResponsePromise, | ||
get(url, options) { | ||
return getDataFromKyResponse(ky.get(url, options)) | ||
}, | ||
post(url, options) { | ||
const data = options.data | ||
delete options.data | ||
const kyOptions: Options = { | ||
...options, | ||
json: data, | ||
} | ||
|
||
return getDataFromKyResponse(ky.post(url, kyOptions)) | ||
}, | ||
put(url, options) { | ||
const data = options.data | ||
delete options.data | ||
const kyOptions: Options = { | ||
...options, | ||
json: data, | ||
} | ||
return getDataFromKyResponse(ky.put(url, kyOptions)) | ||
}, | ||
|
||
patch(url, options) { | ||
const data = options.data | ||
delete options.data | ||
const kyOptions: Options = { | ||
...options, | ||
json: data, | ||
} | ||
return getDataFromKyResponse(ky.patch(url, kyOptions)) | ||
}, | ||
delete(url, options) { | ||
return getDataFromKyResponse(ky.delete(url, options)) | ||
}, | ||
}) | ||
return adaptor | ||
} | ||
|
||
export const createDefaultKyAdaptor = () => createKyAdaptor(ky.create({})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters