@satorijs/adapter-lark 3.7.1 → 3.7.3

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.
@@ -10,12 +10,15 @@ export interface BaseResponse {
10
10
  msg: string
11
11
  }
12
12
 
13
- type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
13
+ export interface InternalConfig {
14
+ multipart?: boolean
15
+ noExtractData?: boolean
16
+ }
14
17
 
15
18
  export class Internal {
16
19
  constructor(private bot: LarkBot) {}
17
20
 
18
- private assertResponse(response: HTTP.Response<BaseResponse>) {
21
+ private _assertResponse(response: HTTP.Response<BaseResponse>) {
19
22
  if (!response.data.code) return
20
23
  this.bot.logger.debug('response: %o', response.data)
21
24
  const error = new HTTP.Error(`request failed`)
@@ -23,10 +26,23 @@ export class Internal {
23
26
  throw error
24
27
  }
25
28
 
26
- static define(routes: Dict<Partial<Record<Method, string | string[]>>>, extractData = true) {
29
+ private _buildData(arg: object, options: InternalConfig) {
30
+ if (options.multipart) {
31
+ const form = new FormData()
32
+ for (const key in arg) {
33
+ const value = arg[key]
34
+ form.append(key, value, value instanceof File ? value.name : undefined)
35
+ }
36
+ return form
37
+ } else {
38
+ return arg
39
+ }
40
+ }
41
+
42
+ static define(routes: Dict<Partial<Record<HTTP.Method, string | string[]>>>, options: InternalConfig = {}) {
27
43
  for (const path in routes) {
28
44
  for (const key in routes[path]) {
29
- const method = key as Method
45
+ const method = key as HTTP.Method
30
46
  for (const name of makeArray(routes[path][method])) {
31
47
  Internal.prototype[name] = async function (this: Internal, ...args: any[]) {
32
48
  const raw = args.join(', ')
@@ -39,17 +55,17 @@ export class Internal {
39
55
  if (method === 'GET' || method === 'DELETE') {
40
56
  config.params = args[0]
41
57
  } else {
42
- config.data = args[0]
58
+ config.data = this._buildData(args[0], options)
43
59
  }
44
60
  } else if (args.length === 2 && method !== 'GET' && method !== 'DELETE') {
45
- config.data = args[0]
61
+ config.data = this._buildData(args[0], options)
46
62
  config.params = args[1]
47
63
  } else if (args.length > 1) {
48
64
  throw new Error(`too many arguments for ${path}, received ${raw}`)
49
65
  }
50
66
  const response = await this.bot.http(method, url, config)
51
- this.assertResponse(response)
52
- return extractData ? response.data.data : response.data
67
+ this._assertResponse(response)
68
+ return options.noExtractData ? response.data : response.data.data
53
69
  }
54
70
  }
55
71
  }