-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from qkang07/feat/kuaishou
Support Kuaishou miniprogram
- Loading branch information
Showing
8 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { InvokeResult } from './types/InvokeResult'; | ||
import { InvokeParams } from './types/InvokeParams'; | ||
import { BatchUploadError, MiniProgramError } from './errors'; | ||
import { fixInvokeParams } from './utils/fixInvokeParams'; | ||
import { Ks } from './types/Ks'; | ||
|
||
declare const ks: Ks; | ||
|
||
const convertResponseType = (responseType?: InvokeParams['responseType']) => { | ||
if (!responseType) return {}; | ||
if (responseType === 'arraybuffer') return { responseType } as const; | ||
if (responseType === 'json') return { dataType: 'json' } as const; | ||
if (responseType === 'text') return { dataType: 'string' } as const; | ||
throw new TypeError(`The responseType "${responseType}" is not supported by Kuaishou Miniprogram`); | ||
}; | ||
|
||
export const requestWithKs = <T>(args: InvokeParams) => | ||
new Promise<InvokeResult<T>>((resolve, reject) => { | ||
const { headers, files, data, responseType, onUploadProgress, signal, ...rest } = fixInvokeParams(args); | ||
const fileNames = files ? Object.keys(files) : []; | ||
|
||
const fail = (obj: unknown) => { | ||
// Perhaps the official is joking with us, so important information actually no documentation :joy:. | ||
const { errCode, errMsg } = Object(obj); | ||
reject(new MiniProgramError(errCode, errMsg)); | ||
}; | ||
|
||
try { | ||
if (fileNames.length === 0) { | ||
/** | ||
* @see https://mp.kuaishou.com/docs/develop/api/network/request/request.html | ||
*/ | ||
const task = ks.request({ | ||
header: headers, | ||
data, | ||
...convertResponseType(responseType), | ||
...rest, | ||
success: ({ header, data, ...rest }) => resolve({ data: data as T, headers: header, ...rest }), | ||
fail, | ||
}); | ||
signal?.addEventListener('abort', () => task.abort()); | ||
} else if (files && fileNames.length === 1) { | ||
if (responseType) { | ||
throw new TypeError('The `responseType` is not supported if `files` not empty in Kuaishou Miniprogram'); | ||
} | ||
const name = fileNames[0]; | ||
const filePath = files[name]; | ||
/** | ||
* @see https://mp.kuaishou.com/docs/develop/api/network/upload/uploadFile.html | ||
*/ | ||
const task = ks.uploadFile({ | ||
header: headers, | ||
formData: data, | ||
name, | ||
filePath, | ||
...rest, | ||
success: ({ header, data, ...rest }) => resolve({ headers: header, data: data as T, ...rest }), | ||
fail, | ||
}); | ||
// Bind onUploadProgress event. | ||
if (onUploadProgress) | ||
task.onProgressUpdate((e) => { | ||
onUploadProgress({ loaded: e.totalBytesSent, total: e.totalBytesExpectedToSend }); | ||
}); | ||
// Bind abort event. | ||
signal?.addEventListener('abort', () => task.abort()); | ||
} else { | ||
throw new BatchUploadError(); | ||
} | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { RequestParams, UploadParams } from '../../types/common'; | ||
import type { Ks } from '../../types/Ks'; | ||
import { UploadTaskImpl } from './UploadTaskImpl'; | ||
import { BaseMpoImpl } from './BaseMpoImpl'; | ||
import { readAsDataURL } from './readAsDataURL'; | ||
import { RequestTaskImpl } from './RequestTaskImpl'; | ||
|
||
class KsConstructor extends BaseMpoImpl implements Ks { | ||
request(req: RequestParams) { | ||
const timer = setTimeout(async () => { | ||
const { header, ...rest } = req; | ||
const { code, msg } = Object(header); | ||
if (req.fail && (code || msg)) { | ||
return req.fail({ errno: Number(code), errMsg: msg }); | ||
} | ||
req.success({ | ||
statusCode: Number(Object(header)['status-code']) || 200, | ||
header: { server: 'mock' }, | ||
data: this.makeData({ ...rest, headers: header }, rest.dataType || rest.responseType), | ||
}); | ||
}); | ||
return new RequestTaskImpl(() => { | ||
clearTimeout(timer); | ||
if (req.fail) req.fail({ errMsg: 'request:fail abort' }); | ||
}); | ||
} | ||
uploadFile(req: UploadParams) { | ||
const timer = setTimeout(async () => { | ||
const { header, name, filePath, formData, ...rest } = req; | ||
req.success({ | ||
statusCode: Number(Object(header)['status-code']) || 200, | ||
header: { server: 'mock' }, | ||
data: { | ||
...rest, | ||
data: formData, | ||
headers: header, | ||
files: { [name]: await readAsDataURL(filePath) }, | ||
}, | ||
}); | ||
}, 100); | ||
return new UploadTaskImpl(() => { | ||
clearTimeout(timer); | ||
if (req.fail) req.fail({ errMsg: 'request:fail abort' }); | ||
}); | ||
} | ||
} | ||
|
||
Object.defineProperty(global, 'ks', { value: new KsConstructor() }); |
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,24 @@ | ||
import { request } from '../..'; | ||
// Kuaishou has just copied wx's base code and has the 'wx' variable left... | ||
import '../libs/mock-wx'; | ||
import '../libs/mock-ks'; | ||
import { requestWithKs } from '../../requestWithKs'; | ||
import { requestWithWx } from '../../requestWithWx'; | ||
|
||
Object.defineProperty(global, 'XMLHttpRequest', { configurable: true, value: null }); | ||
Object.defineProperty(global, 'document', { configurable: true, value: null }); | ||
|
||
jest.mock('../../requestWithKs', () => ({ requestWithKs: jest.fn() })) | ||
jest.mock('../../requestWithWx', () => ({ requestWithWx: jest.fn() })) | ||
|
||
test('look for `ks` object ahead of `wx`', async () => { | ||
|
||
await request({ method: 'GET', url: '/test' }) | ||
expect(requestWithKs).toHaveBeenCalledTimes(1); | ||
expect(requestWithWx).not.toHaveBeenCalled() | ||
|
||
}) | ||
|
||
test(`basic`, async () => { | ||
expect(request({ method: 'GET', url: '/test' })).resolves.not.toBeNull(); | ||
}); |
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
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,6 @@ | ||
import { RequestParams, UploadParams, UploadTask, ProgressInfo, BaseMpo, RequestTask } from './common'; | ||
|
||
export interface Ks extends BaseMpo { | ||
request(req: RequestParams): RequestTask; | ||
uploadFile(req: UploadParams): UploadTask<ProgressInfo>; | ||
} |