@im-more/dsh-dingtalk 0.0.0-stage → 0.1.0
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.
- package/LICENSE +21 -0
- package/cordis.patch.yml +11 -0
- package/lib/index.js +2765 -0
- package/lib/index.js.map +1 -0
- package/lib/types/api/alitrip.d.ts +397 -0
- package/lib/types/api/alitrip.d.ts.map +1 -0
- package/lib/types/api/alitrip.js +21 -0
- package/lib/types/api/alitrip.js.map +1 -0
- package/lib/types/api/attendance.d.ts +63 -0
- package/lib/types/api/attendance.d.ts.map +1 -0
- package/lib/types/api/attendance.js +8 -0
- package/lib/types/api/attendance.js.map +1 -0
- package/lib/types/api/badge.d.ts +233 -0
- package/lib/types/api/badge.d.ts.map +1 -0
- package/lib/types/api/badge.js +19 -0
- package/lib/types/api/badge.js.map +1 -0
- package/lib/types/api/blackboard.d.ts +17 -0
- package/lib/types/api/blackboard.d.ts.map +1 -0
- package/lib/types/api/blackboard.js +6 -0
- package/lib/types/api/blackboard.js.map +1 -0
- package/lib/types/api/calendar.d.ts +621 -0
- package/lib/types/api/calendar.d.ts.map +1 -0
- package/lib/types/api/calendar.js +57 -0
- package/lib/types/api/calendar.js.map +1 -0
- package/lib/types/api/card.d.ts +185 -0
- package/lib/types/api/card.d.ts.map +1 -0
- package/lib/types/api/card.js +13 -0
- package/lib/types/api/card.js.map +1 -0
- package/lib/types/api/conference.d.ts +404 -0
- package/lib/types/api/conference.d.ts.map +1 -0
- package/lib/types/api/conference.js +60 -0
- package/lib/types/api/conference.js.map +1 -0
- package/lib/types/api/connector.d.ts +79 -0
- package/lib/types/api/connector.d.ts.map +1 -0
- package/lib/types/api/connector.js +8 -0
- package/lib/types/api/connector.js.map +1 -0
- package/lib/types/api/contact.d.ts +40 -0
- package/lib/types/api/contact.d.ts.map +1 -0
- package/lib/types/api/contact.js +9 -0
- package/lib/types/api/contact.js.map +1 -0
- package/lib/types/api/convFile.d.ts +135 -0
- package/lib/types/api/convFile.d.ts.map +1 -0
- package/lib/types/api/convFile.js +9 -0
- package/lib/types/api/convFile.js.map +1 -0
- package/lib/types/api/crm.d.ts +662 -0
- package/lib/types/api/crm.d.ts.map +1 -0
- package/lib/types/api/crm.js +59 -0
- package/lib/types/api/crm.js.map +1 -0
- package/lib/types/api/customerService.d.ts +128 -0
- package/lib/types/api/customerService.d.ts.map +1 -0
- package/lib/types/api/customerService.js +13 -0
- package/lib/types/api/customerService.js.map +1 -0
- package/lib/types/api/datacenter.d.ts +506 -0
- package/lib/types/api/datacenter.d.ts.map +1 -0
- package/lib/types/api/datacenter.js +61 -0
- package/lib/types/api/datacenter.js.map +1 -0
- package/lib/types/api/devicemng.d.ts +161 -0
- package/lib/types/api/devicemng.d.ts.map +1 -0
- package/lib/types/api/devicemng.js +20 -0
- package/lib/types/api/devicemng.js.map +1 -0
- package/lib/types/api/diot.d.ts +13 -0
- package/lib/types/api/diot.d.ts.map +1 -0
- package/lib/types/api/diot.js +4 -0
- package/lib/types/api/diot.js.map +1 -0
- package/lib/types/api/doc.d.ts +141 -0
- package/lib/types/api/doc.d.ts.map +1 -0
- package/lib/types/api/doc.js +27 -0
- package/lib/types/api/doc.js.map +1 -0
- package/lib/types/api/drive.d.ts +88 -0
- package/lib/types/api/drive.d.ts.map +1 -0
- package/lib/types/api/drive.js +11 -0
- package/lib/types/api/drive.js.map +1 -0
- package/lib/types/api/edu.d.ts +23 -0
- package/lib/types/api/edu.d.ts.map +1 -0
- package/lib/types/api/edu.js +4 -0
- package/lib/types/api/edu.js.map +1 -0
- package/lib/types/api/esign.d.ts +34 -0
- package/lib/types/api/esign.d.ts.map +1 -0
- package/lib/types/api/esign.js +7 -0
- package/lib/types/api/esign.js.map +1 -0
- package/lib/types/api/exclusive.d.ts +304 -0
- package/lib/types/api/exclusive.d.ts.map +1 -0
- package/lib/types/api/exclusive.js +27 -0
- package/lib/types/api/exclusive.js.map +1 -0
- package/lib/types/api/h3yun.d.ts +438 -0
- package/lib/types/api/h3yun.d.ts.map +1 -0
- package/lib/types/api/h3yun.js +33 -0
- package/lib/types/api/h3yun.js.map +1 -0
- package/lib/types/api/hrm.d.ts +215 -0
- package/lib/types/api/hrm.d.ts.map +1 -0
- package/lib/types/api/hrm.js +20 -0
- package/lib/types/api/hrm.js.map +1 -0
- package/lib/types/api/im.d.ts +773 -0
- package/lib/types/api/im.d.ts.map +1 -0
- package/lib/types/api/im.js +72 -0
- package/lib/types/api/im.js.map +1 -0
- package/lib/types/api/industry.d.ts +118 -0
- package/lib/types/api/industry.d.ts.map +1 -0
- package/lib/types/api/industry.js +14 -0
- package/lib/types/api/industry.js.map +1 -0
- package/lib/types/api/jzcrm.d.ts +255 -0
- package/lib/types/api/jzcrm.d.ts.map +1 -0
- package/lib/types/api/jzcrm.js +18 -0
- package/lib/types/api/jzcrm.js.map +1 -0
- package/lib/types/api/link.d.ts +73 -0
- package/lib/types/api/link.d.ts.map +1 -0
- package/lib/types/api/link.js +9 -0
- package/lib/types/api/link.js.map +1 -0
- package/lib/types/api/live.d.ts +132 -0
- package/lib/types/api/live.d.ts.map +1 -0
- package/lib/types/api/live.js +13 -0
- package/lib/types/api/live.js.map +1 -0
- package/lib/types/api/microApp.d.ts +240 -0
- package/lib/types/api/microApp.d.ts.map +1 -0
- package/lib/types/api/microApp.js +29 -0
- package/lib/types/api/microApp.js.map +1 -0
- package/lib/types/api/oapi.d.ts +3362 -0
- package/lib/types/api/oapi.d.ts.map +1 -0
- package/lib/types/api/oapi.js +247 -0
- package/lib/types/api/oapi.js.map +1 -0
- package/lib/types/api/oauth2.d.ts +115 -0
- package/lib/types/api/oauth2.d.ts.map +1 -0
- package/lib/types/api/oauth2.js +12 -0
- package/lib/types/api/oauth2.js.map +1 -0
- package/lib/types/api/pedia.d.ts +186 -0
- package/lib/types/api/pedia.d.ts.map +1 -0
- package/lib/types/api/pedia.js +13 -0
- package/lib/types/api/pedia.js.map +1 -0
- package/lib/types/api/project.d.ts +1119 -0
- package/lib/types/api/project.d.ts.map +1 -0
- package/lib/types/api/project.js +135 -0
- package/lib/types/api/project.js.map +1 -0
- package/lib/types/api/resident.d.ts +107 -0
- package/lib/types/api/resident.d.ts.map +1 -0
- package/lib/types/api/resident.js +10 -0
- package/lib/types/api/resident.js.map +1 -0
- package/lib/types/api/robot.d.ts +256 -0
- package/lib/types/api/robot.d.ts.map +1 -0
- package/lib/types/api/robot.js +21 -0
- package/lib/types/api/robot.js.map +1 -0
- package/lib/types/api/rooms.d.ts +256 -0
- package/lib/types/api/rooms.d.ts.map +1 -0
- package/lib/types/api/rooms.js +23 -0
- package/lib/types/api/rooms.js.map +1 -0
- package/lib/types/api/serviceGroup.d.ts +176 -0
- package/lib/types/api/serviceGroup.d.ts.map +1 -0
- package/lib/types/api/serviceGroup.js +15 -0
- package/lib/types/api/serviceGroup.js.map +1 -0
- package/lib/types/api/storage.d.ts +1241 -0
- package/lib/types/api/storage.d.ts.map +1 -0
- package/lib/types/api/storage.js +128 -0
- package/lib/types/api/storage.js.map +1 -0
- package/lib/types/api/swform.d.ts +71 -0
- package/lib/types/api/swform.d.ts.map +1 -0
- package/lib/types/api/swform.js +8 -0
- package/lib/types/api/swform.js.map +1 -0
- package/lib/types/api/todo.d.ts +169 -0
- package/lib/types/api/todo.d.ts.map +1 -0
- package/lib/types/api/todo.js +16 -0
- package/lib/types/api/todo.js.map +1 -0
- package/lib/types/api/wiki.d.ts +195 -0
- package/lib/types/api/wiki.d.ts.map +1 -0
- package/lib/types/api/wiki.js +11 -0
- package/lib/types/api/wiki.js.map +1 -0
- package/lib/types/api/workbench.d.ts +46 -0
- package/lib/types/api/workbench.d.ts.map +1 -0
- package/lib/types/api/workbench.js +14 -0
- package/lib/types/api/workbench.js.map +1 -0
- package/lib/types/api/yida.d.ts +1838 -0
- package/lib/types/api/yida.d.ts.map +1 -0
- package/lib/types/api/yida.js +103 -0
- package/lib/types/api/yida.js.map +1 -0
- package/lib/types/bot.d.ts +62 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +134 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +47 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +53 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +36 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +200 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +77 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +249 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/index.d.ts +22 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +22 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/internal.d.ts +15 -0
- package/lib/types/internal.d.ts.map +1 -0
- package/lib/types/internal.js +57 -0
- package/lib/types/internal.js.map +1 -0
- package/lib/types/media.d.ts +57 -0
- package/lib/types/media.d.ts.map +1 -0
- package/lib/types/media.js +99 -0
- package/lib/types/media.js.map +1 -0
- package/lib/types/modes/markdown.d.ts +12 -0
- package/lib/types/modes/markdown.d.ts.map +1 -0
- package/lib/types/modes/markdown.js +148 -0
- package/lib/types/modes/markdown.js.map +1 -0
- package/lib/types/modes/shared.d.ts +183 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +358 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/request.d.ts +63 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +182 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +38 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +128 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/token.d.ts +32 -0
- package/lib/types/token.d.ts.map +1 -0
- package/lib/types/token.js +62 -0
- package/lib/types/token.js.map +1 -0
- package/lib/types/transport/stream.d.ts +89 -0
- package/lib/types/transport/stream.d.ts.map +1 -0
- package/lib/types/transport/stream.js +345 -0
- package/lib/types/transport/stream.js.map +1 -0
- package/lib/types/transport/webhook.d.ts +54 -0
- package/lib/types/transport/webhook.d.ts.map +1 -0
- package/lib/types/transport/webhook.js +159 -0
- package/lib/types/transport/webhook.js.map +1 -0
- package/lib/types/types/index.d.ts +150 -0
- package/lib/types/types/index.d.ts.map +1 -0
- package/lib/types/types/index.js +43 -0
- package/lib/types/types/index.js.map +1 -0
- package/package.json +61 -3
- package/src/api/.eslintrc.yml +2 -0
- package/src/api/alitrip.ts +467 -0
- package/src/api/attendance.ts +81 -0
- package/src/api/badge.ts +285 -0
- package/src/api/blackboard.ts +28 -0
- package/src/api/calendar.ts +817 -0
- package/src/api/card.ts +215 -0
- package/src/api/conference.ts +561 -0
- package/src/api/connector.ts +97 -0
- package/src/api/contact.ts +56 -0
- package/src/api/convFile.ts +166 -0
- package/src/api/crm.ts +830 -0
- package/src/api/customerService.ts +156 -0
- package/src/api/datacenter.ts +672 -0
- package/src/api/devicemng.ts +202 -0
- package/src/api/diot.ts +19 -0
- package/src/api/doc.ts +232 -0
- package/src/api/drive.ts +109 -0
- package/src/api/edu.ts +30 -0
- package/src/api/esign.ts +44 -0
- package/src/api/exclusive.ts +372 -0
- package/src/api/h3yun.ts +537 -0
- package/src/api/hrm.ts +272 -0
- package/src/api/im.ts +978 -0
- package/src/api/industry.ts +153 -0
- package/src/api/jzcrm.ts +304 -0
- package/src/api/link.ts +94 -0
- package/src/api/live.ts +162 -0
- package/src/api/microApp.ts +309 -0
- package/src/api/oapi.ts +4083 -0
- package/src/api/oauth2.ts +146 -0
- package/src/api/pedia.ts +222 -0
- package/src/api/project.ts +1519 -0
- package/src/api/resident.ts +133 -0
- package/src/api/robot.ts +326 -0
- package/src/api/rooms.ts +334 -0
- package/src/api/serviceGroup.ts +216 -0
- package/src/api/storage.ts +1701 -0
- package/src/api/swform.ts +94 -0
- package/src/api/todo.ts +220 -0
- package/src/api/wiki.ts +231 -0
- package/src/api/workbench.ts +73 -0
- package/src/api/yida.ts +2165 -0
- package/src/bot.ts +211 -0
- package/src/config.ts +88 -0
- package/src/decode.ts +250 -0
- package/src/errors.ts +282 -0
- package/src/index.ts +82 -0
- package/src/internal.ts +60 -0
- package/src/media.ts +149 -0
- package/src/modes/markdown.ts +173 -0
- package/src/modes/shared.ts +473 -0
- package/src/request.ts +262 -0
- package/src/send.ts +195 -0
- package/src/token.ts +72 -0
- package/src/transport/stream.ts +436 -0
- package/src/transport/webhook.ts +198 -0
- package/src/types/index.ts +165 -0
- package/README.md +0 -3
package/src/request.ts
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two-root transport every DingTalk REST call shares: URL and version resolution, access-token authentication,
|
|
3
|
+
* envelope unwrapping, and the failure an `errors.ts` verdict classifies.
|
|
4
|
+
*
|
|
5
|
+
* The typed method surface lives on `Internal`; the generated route table calls {@link invoke}, so the migrated layer
|
|
6
|
+
* and the hand-written calls (the Stream registration, the media upload) cannot diverge in authentication or failure
|
|
7
|
+
* handling.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
11
|
+
import type {} from '@deepseek-ai/cordis-plugin-http'
|
|
12
|
+
import type { Config } from './config.ts'
|
|
13
|
+
import { DingtalkApiError, isAuthFailure } from './errors.ts'
|
|
14
|
+
|
|
15
|
+
/** One request body: JSON-shaped data or a multipart form. */
|
|
16
|
+
export type DingtalkRequestBody = object | FormData
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Which of the two roots a request uses.
|
|
20
|
+
*
|
|
21
|
+
* `api` is the `/v1.0/*` OpenAPI (`api.dingtalk.com`): the access token travels in `x-acs-dingtalk-access-token`.
|
|
22
|
+
* `oapi` is the legacy `oapi.dingtalk.com` surface, which takes `access_token` (and the endpoint's own `method`) as
|
|
23
|
+
* query parameters.
|
|
24
|
+
*/
|
|
25
|
+
export type DingtalkBase = 'api' | 'oapi'
|
|
26
|
+
|
|
27
|
+
/** One request, as the generated route table describes it. */
|
|
28
|
+
export interface DingtalkRequestConfig {
|
|
29
|
+
data?: DingtalkRequestBody
|
|
30
|
+
params?: Record<string, unknown> | undefined
|
|
31
|
+
signal?: AbortSignal | undefined
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** What one call needs from the adapter. */
|
|
35
|
+
export interface DingtalkRequestEndpoint {
|
|
36
|
+
readonly ctx: Context
|
|
37
|
+
readonly config: Config
|
|
38
|
+
readonly tokens: {
|
|
39
|
+
get(): Promise<string>
|
|
40
|
+
invalidate(): void
|
|
41
|
+
readonly minting: boolean
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Endpoints that authenticate with the application credentials in their own body, because they mint the token every
|
|
47
|
+
* other `/v1.0/*` call carries. The generated route table keys them without the version segment.
|
|
48
|
+
*/
|
|
49
|
+
const CREDENTIAL_PATHS: ReadonlySet<string> = new Set(['/oauth2/accessToken'])
|
|
50
|
+
|
|
51
|
+
/** How much of a non-JSON body is kept in the error and the log; enough to recognise a gateway page. */
|
|
52
|
+
const BODY_PREFIX_BYTES = 512
|
|
53
|
+
|
|
54
|
+
/** One JSON object, when the value is a plain object. */
|
|
55
|
+
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
56
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
57
|
+
? (value as Record<string, unknown>)
|
|
58
|
+
: undefined
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Message of any caught value, for a log line. */
|
|
62
|
+
function messageOf(error: unknown): string {
|
|
63
|
+
return error instanceof Error ? error.message : String(error)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The full request URL of one endpoint.
|
|
68
|
+
*
|
|
69
|
+
* The generated route table keys paths without the version segment (`/robot/groupMessages/send`), while the
|
|
70
|
+
* hand-written calls name the version root themselves (`/v1.0/gateway/connections/open`); both resolve here.
|
|
71
|
+
*
|
|
72
|
+
* @param config - the adapter configuration holding both roots.
|
|
73
|
+
* @param base - which root the path belongs to.
|
|
74
|
+
* @param path - endpoint path.
|
|
75
|
+
* @returns the absolute request URL.
|
|
76
|
+
*/
|
|
77
|
+
export function endpointOf(
|
|
78
|
+
config: Config,
|
|
79
|
+
base: DingtalkBase,
|
|
80
|
+
path: string,
|
|
81
|
+
): string {
|
|
82
|
+
if (base === 'oapi') return `${config.oapiBaseUrl.replace(/\/+$/, '')}${path}`
|
|
83
|
+
const root = config.apiBaseUrl.replace(/\/+$/, '')
|
|
84
|
+
return `${root}${path.startsWith('/v1.0/') ? path : `/v1.0${path}`}`
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Whether a failed `ctx.http` call was the caller's cancellation rather than a transport failure.
|
|
89
|
+
* @param cause - the value `ctx.http` rejected with.
|
|
90
|
+
* @param signal - the call's signal.
|
|
91
|
+
* @returns true when the caller cancelled the request.
|
|
92
|
+
*/
|
|
93
|
+
function isCancellation(
|
|
94
|
+
cause: unknown,
|
|
95
|
+
signal: AbortSignal | undefined,
|
|
96
|
+
): boolean {
|
|
97
|
+
if (signal?.aborted === true) return true
|
|
98
|
+
if (
|
|
99
|
+
(cause instanceof Error || cause instanceof DOMException) &&
|
|
100
|
+
cause.name === 'AbortError'
|
|
101
|
+
) {
|
|
102
|
+
return true
|
|
103
|
+
}
|
|
104
|
+
return signal?.reason !== undefined && cause === signal.reason
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** A cancelled request rejects with the signal's reason, so the classifier sees the cancellation itself. */
|
|
108
|
+
function cancellation(
|
|
109
|
+
cause: unknown,
|
|
110
|
+
signal: AbortSignal | undefined,
|
|
111
|
+
): unknown {
|
|
112
|
+
return signal?.aborted === true ? signal.reason : cause
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The access token of one request, or undefined for the endpoints that mint it.
|
|
117
|
+
* @param bot - the adapter whose token cache the call uses.
|
|
118
|
+
* @param base - which root the request uses.
|
|
119
|
+
* @param path - the endpoint path.
|
|
120
|
+
* @returns the token to carry, if the endpoint needs one.
|
|
121
|
+
*/
|
|
122
|
+
async function authorization(
|
|
123
|
+
bot: DingtalkRequestEndpoint,
|
|
124
|
+
base: DingtalkBase,
|
|
125
|
+
path: string,
|
|
126
|
+
): Promise<string | undefined> {
|
|
127
|
+
if (base === 'api') {
|
|
128
|
+
// The version root may or may not be part of the path the caller handed over.
|
|
129
|
+
const endpoint = path.startsWith('/v1.0/')
|
|
130
|
+
? path.slice('/v1.0'.length)
|
|
131
|
+
: path
|
|
132
|
+
if (CREDENTIAL_PATHS.has(endpoint)) return undefined
|
|
133
|
+
// The request that mints the token must not wait for the token it is minting.
|
|
134
|
+
if (bot.tokens.minting) return undefined
|
|
135
|
+
}
|
|
136
|
+
return await bot.tokens.get()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Send one request and unwrap its answer. */
|
|
140
|
+
async function send<T>(
|
|
141
|
+
bot: DingtalkRequestEndpoint,
|
|
142
|
+
base: DingtalkBase,
|
|
143
|
+
method: string,
|
|
144
|
+
path: string,
|
|
145
|
+
config: DingtalkRequestConfig,
|
|
146
|
+
token: string | undefined,
|
|
147
|
+
): Promise<T> {
|
|
148
|
+
const url = endpointOf(bot.config, base, path)
|
|
149
|
+
const params: Record<string, unknown> = { ...config.params }
|
|
150
|
+
if (token !== undefined && base === 'oapi') params.access_token = token
|
|
151
|
+
const headers: Record<string, string> = {}
|
|
152
|
+
if (token !== undefined && base === 'api') {
|
|
153
|
+
headers['x-acs-dingtalk-access-token'] = token
|
|
154
|
+
}
|
|
155
|
+
let response: Response
|
|
156
|
+
try {
|
|
157
|
+
response = await bot.ctx.http(url, {
|
|
158
|
+
method,
|
|
159
|
+
...(config.data === undefined ? {} : { data: config.data }),
|
|
160
|
+
...(Object.keys(params).length === 0 ? {} : { params }),
|
|
161
|
+
...(Object.keys(headers).length === 0 ? {} : { headers }),
|
|
162
|
+
...(config.signal === undefined ? {} : { signal: config.signal }),
|
|
163
|
+
})
|
|
164
|
+
} catch (cause) {
|
|
165
|
+
if (isCancellation(cause, config.signal)) {
|
|
166
|
+
throw cancellation(cause, config.signal)
|
|
167
|
+
}
|
|
168
|
+
throw new DingtalkApiError(
|
|
169
|
+
`dingtalk: ${method} ${path} request failed: ${messageOf(cause)}`,
|
|
170
|
+
{ cause },
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
let text: string
|
|
175
|
+
try {
|
|
176
|
+
text = await response.text()
|
|
177
|
+
} catch (cause) {
|
|
178
|
+
// Reading the body fails when the caller cancelled mid-answer; that is still a cancellation.
|
|
179
|
+
if (isCancellation(cause, config.signal)) {
|
|
180
|
+
throw cancellation(cause, config.signal)
|
|
181
|
+
}
|
|
182
|
+
throw new DingtalkApiError(
|
|
183
|
+
`dingtalk: ${method} ${path} answered ${response.status} but its body could not be read`,
|
|
184
|
+
{ status: response.status, cause },
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
let body: unknown
|
|
188
|
+
try {
|
|
189
|
+
body = text === '' ? undefined : JSON.parse(text)
|
|
190
|
+
} catch (error) {
|
|
191
|
+
// The IP-dimension rate limiter answers an HTML page, not JSON; a gateway error page looks the same.
|
|
192
|
+
throw new DingtalkApiError(
|
|
193
|
+
`dingtalk: ${method} ${path} answered ${response.status} without JSON: ${text.slice(0, BODY_PREFIX_BYTES)}`,
|
|
194
|
+
{
|
|
195
|
+
status: response.status,
|
|
196
|
+
nonJson: true,
|
|
197
|
+
bodyText: text.slice(0, BODY_PREFIX_BYTES),
|
|
198
|
+
cause: error,
|
|
199
|
+
},
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const record = asRecord(body)
|
|
204
|
+
const code = record?.code ?? record?.errcode
|
|
205
|
+
const message = record?.message ?? record?.errmsg
|
|
206
|
+
const details = {
|
|
207
|
+
status: response.status,
|
|
208
|
+
...(typeof code === 'string' || typeof code === 'number' ? { code } : {}),
|
|
209
|
+
...(typeof record?.requestid === 'string'
|
|
210
|
+
? { requestid: record.requestid }
|
|
211
|
+
: {}),
|
|
212
|
+
...(text === '' ? {} : { bodyText: text.slice(0, BODY_PREFIX_BYTES) }),
|
|
213
|
+
}
|
|
214
|
+
const suffix =
|
|
215
|
+
(code === undefined ? '' : ` (${String(code)})`) +
|
|
216
|
+
(typeof message === 'string' ? `: ${message}` : '')
|
|
217
|
+
if (response.status < 200 || response.status >= 300) {
|
|
218
|
+
throw new DingtalkApiError(
|
|
219
|
+
`dingtalk: ${method} ${path} answered ${response.status}${suffix}`,
|
|
220
|
+
details,
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
if (code !== undefined && code !== 0) {
|
|
224
|
+
// The legacy `oapi` endpoints answer HTTP 200 with an `errcode`; reading that as a success would lose the refusal.
|
|
225
|
+
throw new DingtalkApiError(
|
|
226
|
+
`dingtalk: ${method} ${path} answered ${response.status}${suffix}`,
|
|
227
|
+
details,
|
|
228
|
+
)
|
|
229
|
+
}
|
|
230
|
+
return body as T
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Call one DingTalk endpoint.
|
|
235
|
+
*
|
|
236
|
+
* A call refused for its access token re-mints the token and is retried once; every other failure is thrown as the
|
|
237
|
+
* `DingtalkApiError` `classify` maps. A cancelled call rejects with the cancellation itself.
|
|
238
|
+
*
|
|
239
|
+
* @param bot - the adapter whose configuration and token cache the call uses.
|
|
240
|
+
* @param base - which root (`/v1.0` OpenAPI or the legacy `oapi`) the path belongs to.
|
|
241
|
+
* @param method - HTTP method.
|
|
242
|
+
* @param path - endpoint path, with or without the `/v1.0` segment.
|
|
243
|
+
* @param config - body, query parameters and cancellation of the call.
|
|
244
|
+
* @returns the unwrapped JSON answer.
|
|
245
|
+
*/
|
|
246
|
+
export async function invoke<T>(
|
|
247
|
+
bot: DingtalkRequestEndpoint,
|
|
248
|
+
base: DingtalkBase,
|
|
249
|
+
method: string,
|
|
250
|
+
path: string,
|
|
251
|
+
config: DingtalkRequestConfig = {},
|
|
252
|
+
): Promise<T> {
|
|
253
|
+
const token = await authorization(bot, base, path)
|
|
254
|
+
try {
|
|
255
|
+
return await send<T>(bot, base, method, path, config, token)
|
|
256
|
+
} catch (error) {
|
|
257
|
+
if (!isAuthFailure(error) || token === undefined) throw error
|
|
258
|
+
bot.tokens.invalidate()
|
|
259
|
+
const fresh = await authorization(bot, base, path)
|
|
260
|
+
return await send<T>(bot, base, method, path, config, fresh)
|
|
261
|
+
}
|
|
262
|
+
}
|
package/src/send.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Outbound calls: one serialized payload becomes one platform message, and one published part becomes one recall.
|
|
3
|
+
*
|
|
4
|
+
* A group message posts to `groupMessages/send` with the open conversation id; a single chat posts to
|
|
5
|
+
* `oToMessages/batchSend` with the recipient's staff id. The response's `processQueryKey` is the only handle a recall
|
|
6
|
+
* accepts, so a send answer without one is reported as an unknown outcome rather than an unpublished one.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
|
|
10
|
+
import type {
|
|
11
|
+
MessageHandle,
|
|
12
|
+
SendContext,
|
|
13
|
+
SentPart,
|
|
14
|
+
Target,
|
|
15
|
+
} from '@deepseek-ai/cordis-plugin-im-core'
|
|
16
|
+
import type { Config } from './config.ts'
|
|
17
|
+
import type { DingtalkPayload } from './modes/shared.ts'
|
|
18
|
+
import type { Internal } from './internal.ts'
|
|
19
|
+
|
|
20
|
+
/** Everything `sendPayload` / `deletePart` need from the adapter. */
|
|
21
|
+
export interface DingtalkSendDeps {
|
|
22
|
+
readonly internal: Internal
|
|
23
|
+
readonly config: Config
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The platform message id of one payload, for the diagnostic record kept in `SentPart.raw`. */
|
|
27
|
+
function mediaIdOf(payload: DingtalkPayload): string | undefined {
|
|
28
|
+
switch (payload.kind) {
|
|
29
|
+
case 'text':
|
|
30
|
+
return undefined
|
|
31
|
+
case 'image':
|
|
32
|
+
return payload.msgParam.photoURL
|
|
33
|
+
case 'audio':
|
|
34
|
+
case 'file':
|
|
35
|
+
return payload.msgParam.mediaId
|
|
36
|
+
case 'video':
|
|
37
|
+
return payload.msgParam.videoMediaId
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The published part of one platform message. */
|
|
42
|
+
function partOf(
|
|
43
|
+
payload: DingtalkPayload,
|
|
44
|
+
processQueryKey: string | undefined,
|
|
45
|
+
referrer: Record<string, string>,
|
|
46
|
+
): SentPart {
|
|
47
|
+
// The request already reached the platform, so an answer without a processQueryKey is a published outcome, not a
|
|
48
|
+
// refusal: the message may be on the platform with no handle left to recall it by.
|
|
49
|
+
if (processQueryKey === undefined || processQueryKey === '') {
|
|
50
|
+
throw new PlatformError(
|
|
51
|
+
'invalid',
|
|
52
|
+
'dingtalk: the send answer carried no processQueryKey',
|
|
53
|
+
{ published: 'unknown' },
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
const mediaId = mediaIdOf(payload)
|
|
57
|
+
return {
|
|
58
|
+
id: processQueryKey,
|
|
59
|
+
kind: 'message',
|
|
60
|
+
time: Date.now(),
|
|
61
|
+
referrer,
|
|
62
|
+
raw: {
|
|
63
|
+
msgKey: payload.msgKey,
|
|
64
|
+
...(mediaId === undefined ? {} : { mediaId }),
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The adapter context one published part reports.
|
|
71
|
+
*
|
|
72
|
+
* The platform has no quoted reply, so the id of the quoted message is recorded for diagnostics only (never rendered).
|
|
73
|
+
*
|
|
74
|
+
* @param conversation - the conversation fields of the send answer.
|
|
75
|
+
* @param ctx - core send context: `quote` is present on every part, and only part 0 records it.
|
|
76
|
+
* @returns the referrer of the published part.
|
|
77
|
+
*/
|
|
78
|
+
function referrerOf(
|
|
79
|
+
conversation: Record<string, string>,
|
|
80
|
+
ctx: SendContext,
|
|
81
|
+
): Record<string, string> {
|
|
82
|
+
return {
|
|
83
|
+
...conversation,
|
|
84
|
+
...(ctx.index === 0 && ctx.quote !== undefined
|
|
85
|
+
? { quotedMessageId: ctx.quote }
|
|
86
|
+
: {}),
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Send one serialized payload.
|
|
92
|
+
*
|
|
93
|
+
* @param deps - adapter services.
|
|
94
|
+
* @param payload - the serialized draft.
|
|
95
|
+
* @param target - the chat the payload goes to.
|
|
96
|
+
* @param ctx - core send context (part index, cancellation, quote).
|
|
97
|
+
* @returns the published part.
|
|
98
|
+
*/
|
|
99
|
+
export async function sendPayload(
|
|
100
|
+
deps: DingtalkSendDeps,
|
|
101
|
+
payload: DingtalkPayload,
|
|
102
|
+
target: Target,
|
|
103
|
+
ctx: SendContext,
|
|
104
|
+
): Promise<SentPart> {
|
|
105
|
+
const msgParam = JSON.stringify(payload.msgParam)
|
|
106
|
+
const robotCode = deps.config.appkey
|
|
107
|
+
if (target.chat.type === 'direct') {
|
|
108
|
+
const answer = await deps.internal.batchSendOTO({
|
|
109
|
+
robotCode,
|
|
110
|
+
userIds: [target.chat.id],
|
|
111
|
+
msgKey: payload.msgKey,
|
|
112
|
+
msgParam,
|
|
113
|
+
})
|
|
114
|
+
const invalid = answer.invalidStaffIdList ?? []
|
|
115
|
+
if (invalid.length > 0) {
|
|
116
|
+
throw new PlatformError(
|
|
117
|
+
'invalid',
|
|
118
|
+
`dingtalk: the recipient cannot receive a robot message (${invalid.join(', ')})`,
|
|
119
|
+
{ published: 'no', code: 'invalidStaffIdList' },
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
const flowControlled = answer.flowControlledStaffIdList ?? []
|
|
123
|
+
if (flowControlled.length > 0) {
|
|
124
|
+
throw new PlatformError(
|
|
125
|
+
'rate-limit',
|
|
126
|
+
`dingtalk: the recipient is flow-controlled (${flowControlled.join(', ')})`,
|
|
127
|
+
{ published: 'no', code: 'flowControlledStaffIdList' },
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
return partOf(
|
|
131
|
+
payload,
|
|
132
|
+
answer.processQueryKey,
|
|
133
|
+
referrerOf({ conversationType: '1', userId: target.chat.id }, ctx),
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
// The platform has no topic concept; a `topic` chat is a group chat with a thread id nothing here produces.
|
|
137
|
+
const answer = await deps.internal.orgGroupSend({
|
|
138
|
+
robotCode,
|
|
139
|
+
openConversationId: target.chat.id,
|
|
140
|
+
msgKey: payload.msgKey,
|
|
141
|
+
msgParam,
|
|
142
|
+
})
|
|
143
|
+
return partOf(
|
|
144
|
+
payload,
|
|
145
|
+
answer.processQueryKey,
|
|
146
|
+
referrerOf(
|
|
147
|
+
{ conversationType: '2', openConversationId: target.chat.id },
|
|
148
|
+
ctx,
|
|
149
|
+
),
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Recall one published message.
|
|
155
|
+
*
|
|
156
|
+
* The recall answers with the ids it accepted; an answer that does not list this part means the message was already
|
|
157
|
+
* recalled, expired past the window, or is not the robot's.
|
|
158
|
+
*
|
|
159
|
+
* @param deps - adapter services.
|
|
160
|
+
* @param part - the published part.
|
|
161
|
+
* @param handle - the message handle (carries the target chat).
|
|
162
|
+
*/
|
|
163
|
+
export async function deletePart(
|
|
164
|
+
deps: DingtalkSendDeps,
|
|
165
|
+
part: SentPart,
|
|
166
|
+
handle: MessageHandle,
|
|
167
|
+
): Promise<void> {
|
|
168
|
+
const robotCode = deps.config.appkey
|
|
169
|
+
if (handle.target.chat.type === 'direct') {
|
|
170
|
+
const answer = await deps.internal.batchRecallOTO({
|
|
171
|
+
robotCode,
|
|
172
|
+
processQueryKeys: [part.id],
|
|
173
|
+
})
|
|
174
|
+
if (!(answer.successResult ?? []).includes(part.id)) {
|
|
175
|
+
throw new PlatformError(
|
|
176
|
+
'not-found',
|
|
177
|
+
`dingtalk: the recall did not accept ${part.id}`,
|
|
178
|
+
{ published: 'no' },
|
|
179
|
+
)
|
|
180
|
+
}
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
const answer = await deps.internal.orgGroupRecall({
|
|
184
|
+
robotCode,
|
|
185
|
+
openConversationId: handle.target.chat.id,
|
|
186
|
+
processQueryKeys: [part.id],
|
|
187
|
+
})
|
|
188
|
+
if (!(answer.successResult ?? []).includes(part.id)) {
|
|
189
|
+
throw new PlatformError(
|
|
190
|
+
'not-found',
|
|
191
|
+
`dingtalk: the recall did not accept ${part.id}`,
|
|
192
|
+
{ published: 'no' },
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
}
|
package/src/token.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The application `accessToken` every `/v1.0/*` request carries.
|
|
3
|
+
*
|
|
4
|
+
* The platform mints one token per application with a two-hour lifetime, and warns against requesting it often, so the
|
|
5
|
+
* cache is lazy (no background timer) and single-flight: concurrent callers share one request. A refusal classified as
|
|
6
|
+
* an authentication failure invalidates the cache and the caller asks again.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** One answer of `POST /v1.0/oauth2/accessToken`. */
|
|
10
|
+
export interface DingtalkAccessToken {
|
|
11
|
+
accessToken?: string
|
|
12
|
+
expireIn?: number
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Seconds shaved off the announced lifetime, so a request never races the expiry. */
|
|
16
|
+
const REFRESH_MARGIN_SECONDS = 300
|
|
17
|
+
|
|
18
|
+
export class AccessTokenCache {
|
|
19
|
+
private current: { value: string; expiresAt: number } | undefined
|
|
20
|
+
private inflight: Promise<string> | undefined
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param mint - requests a new token from the platform.
|
|
24
|
+
* @param now - clock of the expiry check; tests replace it.
|
|
25
|
+
*/
|
|
26
|
+
constructor(
|
|
27
|
+
private readonly mint: () => Promise<DingtalkAccessToken>,
|
|
28
|
+
private readonly now: () => number = Date.now,
|
|
29
|
+
) {}
|
|
30
|
+
|
|
31
|
+
/** The cached token, requesting one when none is cached or the cached one is near its expiry. */
|
|
32
|
+
async get(): Promise<string> {
|
|
33
|
+
const current = this.current
|
|
34
|
+
if (current !== undefined && this.now() < current.expiresAt)
|
|
35
|
+
return current.value
|
|
36
|
+
this.inflight ??= this.mint()
|
|
37
|
+
.then((answer) => {
|
|
38
|
+
if (answer.accessToken === undefined || answer.accessToken === '') {
|
|
39
|
+
throw new Error('dingtalk: getAccessToken answered without a token')
|
|
40
|
+
}
|
|
41
|
+
const expireIn = answer.expireIn
|
|
42
|
+
// Without a usable lifetime the token is still returned to this round of callers but not cached: a `NaN`
|
|
43
|
+
// deadline would silently re-request on every call.
|
|
44
|
+
if (typeof expireIn === 'number' && Number.isFinite(expireIn)) {
|
|
45
|
+
this.current = {
|
|
46
|
+
value: answer.accessToken,
|
|
47
|
+
expiresAt: this.now() + (expireIn - REFRESH_MARGIN_SECONDS) * 1_000,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return answer.accessToken
|
|
51
|
+
})
|
|
52
|
+
.finally(() => {
|
|
53
|
+
this.inflight = undefined
|
|
54
|
+
})
|
|
55
|
+
return await this.inflight
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Drop the cached token, so the next {@link get} asks the platform again. */
|
|
59
|
+
invalidate(): void {
|
|
60
|
+
this.current = undefined
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The token in the cache, when one is cached; for diagnostics only. */
|
|
64
|
+
get cached(): string | undefined {
|
|
65
|
+
return this.current?.value
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Whether a token request is in flight, so the transport can leave the minting request itself unauthenticated. */
|
|
69
|
+
get minting(): boolean {
|
|
70
|
+
return this.inflight !== undefined
|
|
71
|
+
}
|
|
72
|
+
}
|