Skip to content
This repository has been archived by the owner on Dec 20, 2022. It is now read-only.

Commit

Permalink
fix: ky adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Innei committed Nov 4, 2022
1 parent 470572d commit 039a0b0
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 6 deletions.
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion src/__tests__/adaptors/ky.spec.ts
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)
})
72 changes: 72 additions & 0 deletions src/adaptors/ky-universal.ts
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({}))
6 changes: 1 addition & 5 deletions src/adaptors/ky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import type { KyInstance } from 'ky/distribution/types/ky'

import type { IRequestAdapter } from '~/interfaces/adapter'

// eslint-disable-next-line spaced-comment
const $http: KyInstance = /*#__PURE__*/ ky.create({})
// TODO post data not only json,
const getDataFromKyResponse = async (response: ResponsePromise) => {
const res = await response
Expand Down Expand Up @@ -71,6 +69,4 @@ export const createKyAdaptor = (ky: KyInstance) => {
return adaptor
}

export const defaultKyAdaptor = createKyAdaptor($http)

export default defaultKyAdaptor
export const createDefaultKyAdaptor = () => createKyAdaptor(ky.create({}))
1 change: 1 addition & 0 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"isomorphic-unfetch": "3.1.0",
"jsdom": "20.0.1",
"ky": "0.31.4",
"ky-universal": "^0.11.0",
"lodash": "4.17.21",
"node-fetch": "3.2.10",
"rollup": "2.79.1",
Expand Down

0 comments on commit 039a0b0

Please sign in to comment.