Skip to content

Commit

Permalink
feat(client-sdk): support taro request (labring#1325)
Browse files Browse the repository at this point in the history
* docs: update trigger.md

* feat(client-sdk): add taro request support
  • Loading branch information
seewhy163 authored Jun 28, 2023
1 parent f46a8a0 commit 021adbd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/client-sdk/src/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Db } from "database-ql";
import { Request } from "./request/request";
import { UniRequest } from "./request/request-uni";
import { WxmpRequest } from "./request/request-wxmp";
import { TaroRequest } from "./request/request-taro";
import { CloudOptions, EnvironmentType, RequestInterface } from "./types";

interface GlobalObjectType {
Expand Down Expand Up @@ -32,6 +33,8 @@ class Cloud {
}
} else if (env === EnvironmentType.WX_MP) {
ret = WxmpRequest;
} else if (env === EnvironmentType.TARO) {
ret = TaroRequest;
} else {
ret = Request;
}
Expand Down
42 changes: 42 additions & 0 deletions packages/client-sdk/src/request/request-taro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Request } from './request'
import { CloudOptions, EnvironmentType } from '../types'

interface GlobalObjectType {
request: any
uploadFile: any
}
declare const taro: GlobalObjectType

/**
* Taro 环境请求类
*/
export class TaroRequest extends Request {
constructor(config: CloudOptions) {
super(config)
}

/**
* Taro 环境请求方法
* @override
* @param data
* @returns
*/
async request(url: string, data: any, _options?: any) {
if (this.options.environment !== EnvironmentType.TARO) {
throw new Error('environment type must be taro')
}

const token = this.options.getAccessToken()
const header = this.getHeaders(token)
const options = {
url,
header,
method: _options?.method ?? 'POST',
data,
dataType: 'json'
}

const res = await taro.request(options)
return res
}
}
3 changes: 2 additions & 1 deletion packages/client-sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export interface RequestInterface extends BaseRequestInterface {
export enum EnvironmentType {
H5 = 'h5',
WX_MP = 'wxmp',
UNI_APP = 'uniapp'
UNI_APP = 'uniapp',
TARO = 'taro'
}

type GetAccessTokenFuncType = () => string
Expand Down

0 comments on commit 021adbd

Please sign in to comment.