@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
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nodes, drafts, payloads and text rules of the single `markdown` mode.
|
|
3
|
+
*
|
|
4
|
+
* DingTalk renders one body per outgoing message from `msgKey` + `msgParam`, and a message carries at most one media
|
|
5
|
+
* item, so media leaves the text run and becomes a message of its own (as the upstream encoder did by splitting an image
|
|
6
|
+
* out of the markdown).
|
|
7
|
+
*/
|
|
8
|
+
import { isPhrasingContainer } from '@deepseek-ai/satori-element';
|
|
9
|
+
/** Zero-width non-joiner: a body the platform accepts where the content is empty. */
|
|
10
|
+
export const ZERO_WIDTH = '\u2060';
|
|
11
|
+
/** The two bounds the platform documents, and nothing else. */
|
|
12
|
+
export const DINGTALK_LIMITS = {
|
|
13
|
+
/**
|
|
14
|
+
* `msgParam` is capped at 15000 bytes ("长度限制 15000 字节以内"). The adapter measures the UTF-8 bytes of the JSON
|
|
15
|
+
* body it sends; a platform that counted characters instead would only make an accepted body shorter.
|
|
16
|
+
*/
|
|
17
|
+
payloadBytes: 15_000,
|
|
18
|
+
/** A single-chat message can be recalled within 24 hours; the group endpoint documents no other window. */
|
|
19
|
+
deleteWindowMs: 24 * 60 * 60 * 1_000,
|
|
20
|
+
};
|
|
21
|
+
/** Title length; the notification and the conversation list show it. */
|
|
22
|
+
const TITLE_LENGTH = 40;
|
|
23
|
+
/**
|
|
24
|
+
* Escape text so DingTalk markdown renders it literally.
|
|
25
|
+
*
|
|
26
|
+
* Four rules, identical to the upstream encoder's: a markup character gets a zero-width space, markdown punctuation
|
|
27
|
+
* gets a backslash, a list marker after a space is broken up, and four leading spaces (a code block) are replaced.
|
|
28
|
+
*
|
|
29
|
+
* @param text - the text of a `text` element.
|
|
30
|
+
* @returns the text to append to the markdown body.
|
|
31
|
+
*/
|
|
32
|
+
export function escape(text) {
|
|
33
|
+
return text
|
|
34
|
+
.replace(/(?<!\u200B)[*_~`]/g, '\u200B$&')
|
|
35
|
+
.replace(/([\\`*_{}[\]\-(#!>])/g, '\\$&')
|
|
36
|
+
.replace(/([\-*]|\d\.) /g, '\u200B$&')
|
|
37
|
+
.replace(/^(\s{4})/gm, '\u200B ');
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Undo {@link escape}.
|
|
41
|
+
*
|
|
42
|
+
* DingTalk never echoes what the adapter sent, so this exists for the inbound side and to keep a round trip of the
|
|
43
|
+
* upstream rules testable.
|
|
44
|
+
*
|
|
45
|
+
* @param text - escaped text.
|
|
46
|
+
* @returns the text as it was written.
|
|
47
|
+
*/
|
|
48
|
+
export function unescape(text) {
|
|
49
|
+
return text.replace(/\u200B([*_~`])/g, '$1');
|
|
50
|
+
}
|
|
51
|
+
/** The visible text of one line of markdown, with the notation this adapter emits removed. */
|
|
52
|
+
function plainLine(line) {
|
|
53
|
+
return line
|
|
54
|
+
.replace(/\u200B/g, '')
|
|
55
|
+
.replace(/\\(.)/g, '$1')
|
|
56
|
+
.replace(/\[([^\]]*)\]\([^)]*\)/g, '$1')
|
|
57
|
+
.replace(/`/g, '')
|
|
58
|
+
.replace(/\*\*|__/g, '')
|
|
59
|
+
.replace(/[*_~]/g, '')
|
|
60
|
+
.replace(/^#+\s*/, '')
|
|
61
|
+
.replace(/^\d+\.\s+/, '')
|
|
62
|
+
.replace(/^[-*]\s+/, '')
|
|
63
|
+
.replace(/^>\s?/, '');
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The `title` the `sampleMarkdown` template requires.
|
|
67
|
+
*
|
|
68
|
+
* The error table names `miss.param.markdownTotitle`, so the field is sent even though the upstream adapter left it
|
|
69
|
+
* out: the first non-empty line, its markdown notation removed, cut to 40 characters.
|
|
70
|
+
*
|
|
71
|
+
* @param text - the body of the draft, before the paragraph newlines are doubled.
|
|
72
|
+
* @returns the title, or a zero-width body when the message has no visible text.
|
|
73
|
+
*/
|
|
74
|
+
export function titleOf(text) {
|
|
75
|
+
const line = text.split('\n').find((item) => item.trim() !== '');
|
|
76
|
+
if (line === undefined)
|
|
77
|
+
return ZERO_WIDTH;
|
|
78
|
+
const plain = plainLine(line).trim();
|
|
79
|
+
if (plain === '')
|
|
80
|
+
return ZERO_WIDTH;
|
|
81
|
+
// Code points, not UTF-16 units: a title must not end in half a surrogate pair.
|
|
82
|
+
return [...plain].slice(0, TITLE_LENGTH).join('');
|
|
83
|
+
}
|
|
84
|
+
/** DingTalk markdown separates paragraphs with a blank line; this is the only place newlines are doubled. */
|
|
85
|
+
function paragraphBreaks(text) {
|
|
86
|
+
return text.replace(/\n/g, '\n\n');
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Media element → the node of its own platform message.
|
|
90
|
+
*
|
|
91
|
+
* A DingTalk message carries one media item, so media never stays inside a text run. Media that stood in phrasing
|
|
92
|
+
* content (`<p>`, a heading, a link, an inline wrapper) is reported as `moved`: the caller sees the text plus an extra
|
|
93
|
+
* media message instead of one block.
|
|
94
|
+
*
|
|
95
|
+
* @param el - one of `img`/`audio`/`video`/`file`.
|
|
96
|
+
* @param ctx - render context of the mode.
|
|
97
|
+
* @returns the media node, published after the text run it was found in.
|
|
98
|
+
*/
|
|
99
|
+
export function mediaNode(el, ctx) {
|
|
100
|
+
const parent = ctx.ancestors[ctx.ancestors.length - 1];
|
|
101
|
+
if (parent !== undefined && isPhrasingContainer(parent.type)) {
|
|
102
|
+
ctx.report.moved.push({
|
|
103
|
+
tag: el.type,
|
|
104
|
+
reason: 'dingtalk-media-split',
|
|
105
|
+
path: ctx.stableId(el),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
switch (el.type) {
|
|
109
|
+
case 'img':
|
|
110
|
+
return {
|
|
111
|
+
type: 'media',
|
|
112
|
+
media: { kind: 'image', ref: ctx.resource('image', el.attrs.src, {}) },
|
|
113
|
+
};
|
|
114
|
+
case 'audio':
|
|
115
|
+
return {
|
|
116
|
+
type: 'media',
|
|
117
|
+
media: audioMedia(el, ctx),
|
|
118
|
+
};
|
|
119
|
+
case 'video':
|
|
120
|
+
return {
|
|
121
|
+
type: 'media',
|
|
122
|
+
media: {
|
|
123
|
+
kind: 'video',
|
|
124
|
+
ref: ctx.resource('video', el.attrs.src, {
|
|
125
|
+
...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
|
|
126
|
+
...(el.attrs.duration === undefined
|
|
127
|
+
? {}
|
|
128
|
+
: { duration: el.attrs.duration }),
|
|
129
|
+
}),
|
|
130
|
+
...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
|
|
131
|
+
...(el.attrs.duration === undefined
|
|
132
|
+
? {}
|
|
133
|
+
: { duration: el.attrs.duration }),
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
case 'file':
|
|
137
|
+
return {
|
|
138
|
+
type: 'media',
|
|
139
|
+
media: {
|
|
140
|
+
kind: 'file',
|
|
141
|
+
ref: ctx.resource('file', el.attrs.src, el.attrs.name === undefined ? {} : { name: el.attrs.name }),
|
|
142
|
+
...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/** Audio has the same fields as video without a poster, split out so the switch above stays readable. */
|
|
148
|
+
function audioMedia(el, ctx) {
|
|
149
|
+
return {
|
|
150
|
+
kind: 'audio',
|
|
151
|
+
ref: ctx.resource('audio', el.attrs.src, {
|
|
152
|
+
...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
|
|
153
|
+
...(el.attrs.duration === undefined
|
|
154
|
+
? {}
|
|
155
|
+
: { duration: el.attrs.duration }),
|
|
156
|
+
}),
|
|
157
|
+
...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
|
|
158
|
+
...(el.attrs.duration === undefined ? {} : { duration: el.attrs.duration }),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/** One text run being accumulated, in send order. */
|
|
162
|
+
class Run {
|
|
163
|
+
buffer = '';
|
|
164
|
+
/** The text so far, and the media that has to go out before the next text draft. */
|
|
165
|
+
drafts = [];
|
|
166
|
+
/** Append text. */
|
|
167
|
+
push(content) {
|
|
168
|
+
this.buffer += content;
|
|
169
|
+
}
|
|
170
|
+
/** Close the current line unless it is already closed. */
|
|
171
|
+
breakLine() {
|
|
172
|
+
if (this.buffer === '' || this.buffer.endsWith('\n'))
|
|
173
|
+
return;
|
|
174
|
+
this.buffer += '\n';
|
|
175
|
+
}
|
|
176
|
+
/** Emit the accumulated text as a draft of its own, then the media. */
|
|
177
|
+
flush(next) {
|
|
178
|
+
const text = this.buffer.trim();
|
|
179
|
+
this.buffer = '';
|
|
180
|
+
if (text !== '')
|
|
181
|
+
this.drafts.push({ kind: 'text', text });
|
|
182
|
+
if (next !== undefined)
|
|
183
|
+
this.drafts.push({ kind: 'media', media: next });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Fold a node stream into platform messages: a text draft per run, and one draft per media item.
|
|
188
|
+
*
|
|
189
|
+
* DingTalk sends one media item per message, so a run of text followed by an image is two platform messages; the run is
|
|
190
|
+
* split rather than merged into the image, because no template carries both.
|
|
191
|
+
*
|
|
192
|
+
* @param nodes - rendered nodes, in source order.
|
|
193
|
+
* @returns at least one draft; empty content yields an empty text draft.
|
|
194
|
+
*/
|
|
195
|
+
export function assembleDraft(nodes) {
|
|
196
|
+
const run = new Run();
|
|
197
|
+
for (const node of nodes) {
|
|
198
|
+
switch (node.type) {
|
|
199
|
+
case 'text':
|
|
200
|
+
run.push(node.content);
|
|
201
|
+
break;
|
|
202
|
+
case 'break':
|
|
203
|
+
run.breakLine();
|
|
204
|
+
break;
|
|
205
|
+
case 'media':
|
|
206
|
+
run.flush(node.media);
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
run.flush();
|
|
211
|
+
if (run.drafts.length === 0)
|
|
212
|
+
run.drafts.push({ kind: 'text', text: '' });
|
|
213
|
+
return run.drafts;
|
|
214
|
+
}
|
|
215
|
+
/** The upload's own report of a media item (`extra`), when the adapter recorded one. */
|
|
216
|
+
function extraOf(value) {
|
|
217
|
+
if (typeof value !== 'object' || value === null)
|
|
218
|
+
return undefined;
|
|
219
|
+
const name = 'name' in value && typeof value.name === 'string' ? value.name : undefined;
|
|
220
|
+
const mime = 'mime' in value && typeof value.mime === 'string' ? value.mime : undefined;
|
|
221
|
+
if (name === undefined && mime === undefined)
|
|
222
|
+
return undefined;
|
|
223
|
+
return {
|
|
224
|
+
...(name === undefined ? {} : { name }),
|
|
225
|
+
...(mime === undefined ? {} : { mime }),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
/** Resolve one media item of a prepared draft. */
|
|
229
|
+
function resolvedMedia(prepared, media) {
|
|
230
|
+
const resolved = prepared.resolved.get(media.ref);
|
|
231
|
+
if (resolved === undefined) {
|
|
232
|
+
throw new Error('dingtalk: media was not prepared before serialize');
|
|
233
|
+
}
|
|
234
|
+
const extra = extraOf(resolved.extra);
|
|
235
|
+
return {
|
|
236
|
+
key: resolved.key,
|
|
237
|
+
name: media.name ?? extra?.name,
|
|
238
|
+
mime: media.ref.meta.mime ?? extra?.mime,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
/** File extension of a name, without the dot. */
|
|
242
|
+
function extensionOf(name) {
|
|
243
|
+
if (name === undefined)
|
|
244
|
+
return undefined;
|
|
245
|
+
const dot = name.lastIndexOf('.');
|
|
246
|
+
if (dot <= 0 || dot === name.length - 1)
|
|
247
|
+
return undefined;
|
|
248
|
+
return name.slice(dot + 1).toLowerCase();
|
|
249
|
+
}
|
|
250
|
+
/** Media type of a MIME type: the subtype, without any `+suffix`. */
|
|
251
|
+
function subtypeOf(mime) {
|
|
252
|
+
if (mime === undefined)
|
|
253
|
+
return undefined;
|
|
254
|
+
const subtype = mime.split(';')[0].split('/')[1];
|
|
255
|
+
if (subtype === undefined)
|
|
256
|
+
return undefined;
|
|
257
|
+
return subtype.replace(/\+.*$/, '');
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* The `fileType` field of `sampleFile`.
|
|
261
|
+
* @param name - file name of the attachment.
|
|
262
|
+
* @param mime - MIME type the upload detected.
|
|
263
|
+
* @returns the extension of the name, else the MIME subtype, else `file`.
|
|
264
|
+
*/
|
|
265
|
+
export function fileTypeOf(name, mime) {
|
|
266
|
+
return extensionOf(name) ?? subtypeOf(mime) ?? 'file';
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Build the request payload of one draft.
|
|
270
|
+
*
|
|
271
|
+
* The paragraph newlines are doubled here and nowhere else: `assemble` keeps single newlines, so a text that already
|
|
272
|
+
* contains them cannot be doubled twice.
|
|
273
|
+
*
|
|
274
|
+
* @param prepared - the draft with every resource resolved.
|
|
275
|
+
* @param _ctx - serialize context (the adapter needs nothing from it; quotes are not rendered, see the design).
|
|
276
|
+
* @returns the exact `msgKey` + `msgParam` pair of the message.
|
|
277
|
+
*/
|
|
278
|
+
export function serializeDraft(prepared, _ctx) {
|
|
279
|
+
const draft = prepared.draft;
|
|
280
|
+
if (draft.kind === 'text') {
|
|
281
|
+
const text = paragraphBreaks(draft.text);
|
|
282
|
+
return {
|
|
283
|
+
kind: 'text',
|
|
284
|
+
msgKey: 'sampleMarkdown',
|
|
285
|
+
msgParam: {
|
|
286
|
+
title: titleOf(draft.text),
|
|
287
|
+
text: text === '' ? ZERO_WIDTH : text,
|
|
288
|
+
},
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
const media = resolvedMedia(prepared, draft.media);
|
|
292
|
+
switch (draft.media.kind) {
|
|
293
|
+
case 'image':
|
|
294
|
+
return {
|
|
295
|
+
kind: 'image',
|
|
296
|
+
msgKey: 'sampleImageMsg',
|
|
297
|
+
msgParam: { photoURL: media.key },
|
|
298
|
+
};
|
|
299
|
+
case 'audio':
|
|
300
|
+
return {
|
|
301
|
+
kind: 'audio',
|
|
302
|
+
msgKey: 'sampleAudio',
|
|
303
|
+
msgParam: {
|
|
304
|
+
mediaId: media.key,
|
|
305
|
+
duration: String(draft.media.duration ?? 0),
|
|
306
|
+
},
|
|
307
|
+
};
|
|
308
|
+
case 'video':
|
|
309
|
+
return {
|
|
310
|
+
kind: 'video',
|
|
311
|
+
msgKey: 'sampleVideo',
|
|
312
|
+
msgParam: {
|
|
313
|
+
duration: String(draft.media.duration ?? 0),
|
|
314
|
+
videoMediaId: media.key,
|
|
315
|
+
videoType: 'mp4',
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
case 'file':
|
|
319
|
+
return {
|
|
320
|
+
kind: 'file',
|
|
321
|
+
msgKey: 'sampleFile',
|
|
322
|
+
msgParam: {
|
|
323
|
+
mediaId: media.key,
|
|
324
|
+
fileName: media.name ?? 'file',
|
|
325
|
+
fileType: fileTypeOf(media.name, media.mime),
|
|
326
|
+
},
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Hard platform limits of one draft, checked before any request.
|
|
332
|
+
*
|
|
333
|
+
* Only `msgParam` can fail: a text body over 15000 bytes is refused by the platform, and truncating it would silently
|
|
334
|
+
* drop model output. A media item's `msgParam` is a handful of ids and never reaches the bound.
|
|
335
|
+
*
|
|
336
|
+
* @param draft - the assembled draft.
|
|
337
|
+
* @returns every violation the platform would refuse.
|
|
338
|
+
*/
|
|
339
|
+
export function checkDraft(draft) {
|
|
340
|
+
if (draft.kind !== 'text')
|
|
341
|
+
return [];
|
|
342
|
+
const bytes = Buffer.byteLength(JSON.stringify({
|
|
343
|
+
title: titleOf(draft.text),
|
|
344
|
+
text: paragraphBreaks(draft.text) || ZERO_WIDTH,
|
|
345
|
+
}), 'utf8');
|
|
346
|
+
if (bytes <= DINGTALK_LIMITS.payloadBytes)
|
|
347
|
+
return [];
|
|
348
|
+
return [
|
|
349
|
+
{
|
|
350
|
+
kind: 'size',
|
|
351
|
+
key: 'limit.payload.bytes',
|
|
352
|
+
limit: DINGTALK_LIMITS.payloadBytes,
|
|
353
|
+
actual: bytes,
|
|
354
|
+
message: `DingTalk accepts ${DINGTALK_LIMITS.payloadBytes} bytes of msgParam, got ${bytes}`,
|
|
355
|
+
},
|
|
356
|
+
];
|
|
357
|
+
}
|
|
358
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../src/modes/shared.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAWH,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAA;AAGjE,qFAAqF;AACrF,MAAM,CAAC,MAAM,UAAU,GAAG,QAAQ,CAAA;AAmElC,+DAA+D;AAC/D,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B;;;OAGG;IACH,YAAY,EAAE,MAAM;IACpB,2GAA2G;IAC3G,cAAc,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,KAAK;CAC5B,CAAA;AAEV,wEAAwE;AACxE,MAAM,YAAY,GAAG,EAAE,CAAA;AAEvB;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM,CAAC,IAAY;IACjC,OAAO,IAAI;SACR,OAAO,CAAC,oBAAoB,EAAE,UAAU,CAAC;SACzC,OAAO,CAAC,uBAAuB,EAAE,MAAM,CAAC;SACxC,OAAO,CAAC,gBAAgB,EAAE,UAAU,CAAC;SACrC,OAAO,CAAC,YAAY,EAAE,gCAAgC,CAAC,CAAA;AAC5D,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,QAAQ,CAAC,IAAY;IACnC,O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AAS,CAAC;QACb,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC;QAC1B,IAAI,EAAE,eAAe,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,UAAU;KAChD,CAAC,EACF,MAAM,CACP,CAAA;IACD,IAAI,KAAK,IAAI,eAAe,CAAC,YAAY;QAAE,OAAO,EAAE,CAAA;IACpD,OAAO;QACL;YACE,IAAI,EAAE,MAAM;YACZ,GAAG,EAAE,qBAAqB;YAC1B,KAAK,EAAE,eAAe,CAAC,YAAY;YACnC,MAAM,EAAE,KAAK;YACb,OAAO,EAAE,oBAAoB,eAAe,CAAC,YAAY,2BAA2B,KAAK,EAAE;SAC5F;KACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
10
|
+
import type { Config } from './config.ts';
|
|
11
|
+
/** One request body: JSON-shaped data or a multipart form. */
|
|
12
|
+
export type DingtalkRequestBody = object | FormData;
|
|
13
|
+
/**
|
|
14
|
+
* Which of the two roots a request uses.
|
|
15
|
+
*
|
|
16
|
+
* `api` is the `/v1.0/*` OpenAPI (`api.dingtalk.com`): the access token travels in `x-acs-dingtalk-access-token`.
|
|
17
|
+
* `oapi` is the legacy `oapi.dingtalk.com` surface, which takes `access_token` (and the endpoint's own `method`) as
|
|
18
|
+
* query parameters.
|
|
19
|
+
*/
|
|
20
|
+
export type DingtalkBase = 'api' | 'oapi';
|
|
21
|
+
/** One request, as the generated route table describes it. */
|
|
22
|
+
export interface DingtalkRequestConfig {
|
|
23
|
+
data?: DingtalkRequestBody;
|
|
24
|
+
params?: Record<string, unknown> | undefined;
|
|
25
|
+
signal?: AbortSignal | undefined;
|
|
26
|
+
}
|
|
27
|
+
/** What one call needs from the adapter. */
|
|
28
|
+
export interface DingtalkRequestEndpoint {
|
|
29
|
+
readonly ctx: Context;
|
|
30
|
+
readonly config: Config;
|
|
31
|
+
readonly tokens: {
|
|
32
|
+
get(): Promise<string>;
|
|
33
|
+
invalidate(): void;
|
|
34
|
+
readonly minting: boolean;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The full request URL of one endpoint.
|
|
39
|
+
*
|
|
40
|
+
* The generated route table keys paths without the version segment (`/robot/groupMessages/send`), while the
|
|
41
|
+
* hand-written calls name the version root themselves (`/v1.0/gateway/connections/open`); both resolve here.
|
|
42
|
+
*
|
|
43
|
+
* @param config - the adapter configuration holding both roots.
|
|
44
|
+
* @param base - which root the path belongs to.
|
|
45
|
+
* @param path - endpoint path.
|
|
46
|
+
* @returns the absolute request URL.
|
|
47
|
+
*/
|
|
48
|
+
export declare function endpointOf(config: Config, base: DingtalkBase, path: string): string;
|
|
49
|
+
/**
|
|
50
|
+
* Call one DingTalk endpoint.
|
|
51
|
+
*
|
|
52
|
+
* A call refused for its access token re-mints the token and is retried once; every other failure is thrown as the
|
|
53
|
+
* `DingtalkApiError` `classify` maps. A cancelled call rejects with the cancellation itself.
|
|
54
|
+
*
|
|
55
|
+
* @param bot - the adapter whose configuration and token cache the call uses.
|
|
56
|
+
* @param base - which root (`/v1.0` OpenAPI or the legacy `oapi`) the path belongs to.
|
|
57
|
+
* @param method - HTTP method.
|
|
58
|
+
* @param path - endpoint path, with or without the `/v1.0` segment.
|
|
59
|
+
* @param config - body, query parameters and cancellation of the call.
|
|
60
|
+
* @returns the unwrapped JSON answer.
|
|
61
|
+
*/
|
|
62
|
+
export declare function invoke<T>(bot: DingtalkRequestEndpoint, base: DingtalkBase, method: string, path: string, config?: DingtalkRequestConfig): Promise<T>;
|
|
63
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAElD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAGzC,8DAA8D;AAC9D,MAAM,MAAM,mBAAmB,GAAG,MAAM,GAAG,QAAQ,CAAA;AAEnD;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG,MAAM,CAAA;AAEzC,8DAA8D;AAC9D,MAAM,WAAW,qBAAqB;IACpC,IAAI,CAAC,EAAE,mBAAmB,CAAA;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAA;IAC5C,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;CACjC;AAED,4CAA4C;AAC5C,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAA;IACrB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,MAAM,EAAE;QACf,GAAG,IAAI,OAAO,CAAC,MAAM,CAAC,CAAA;QACtB,UAAU,IAAI,IAAI,CAAA;QAClB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;KAC1B,CAAA;CACF;AAuBD;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,YAAY,EAClB,IAAI,EAAE,MAAM,GACX,MAAM,CAIR;AAoJD;;;;;;;;;;;;GAYG;AACH,wBAAsB,MAAM,CAAC,CAAC,EAC5B,GAAG,EAAE,uBAAuB,EAC5B,IAAI,EAAE,YAAY,EAClB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,MAAM,GAAE,qBAA0B,GACjC,OAAO,CAAC,CAAC,CAAC,CAUZ"}
|
|
@@ -0,0 +1,182 @@
|
|
|
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
|
+
import { DingtalkApiError, isAuthFailure } from "./errors.js";
|
|
10
|
+
/**
|
|
11
|
+
* Endpoints that authenticate with the application credentials in their own body, because they mint the token every
|
|
12
|
+
* other `/v1.0/*` call carries. The generated route table keys them without the version segment.
|
|
13
|
+
*/
|
|
14
|
+
const CREDENTIAL_PATHS = new Set(['/oauth2/accessToken']);
|
|
15
|
+
/** How much of a non-JSON body is kept in the error and the log; enough to recognise a gateway page. */
|
|
16
|
+
const BODY_PREFIX_BYTES = 512;
|
|
17
|
+
/** One JSON object, when the value is a plain object. */
|
|
18
|
+
function asRecord(value) {
|
|
19
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
20
|
+
? value
|
|
21
|
+
: undefined;
|
|
22
|
+
}
|
|
23
|
+
/** Message of any caught value, for a log line. */
|
|
24
|
+
function messageOf(error) {
|
|
25
|
+
return error instanceof Error ? error.message : String(error);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The full request URL of one endpoint.
|
|
29
|
+
*
|
|
30
|
+
* The generated route table keys paths without the version segment (`/robot/groupMessages/send`), while the
|
|
31
|
+
* hand-written calls name the version root themselves (`/v1.0/gateway/connections/open`); both resolve here.
|
|
32
|
+
*
|
|
33
|
+
* @param config - the adapter configuration holding both roots.
|
|
34
|
+
* @param base - which root the path belongs to.
|
|
35
|
+
* @param path - endpoint path.
|
|
36
|
+
* @returns the absolute request URL.
|
|
37
|
+
*/
|
|
38
|
+
export function endpointOf(config, base, path) {
|
|
39
|
+
if (base === 'oapi')
|
|
40
|
+
return `${config.oapiBaseUrl.replace(/\/+$/, '')}${path}`;
|
|
41
|
+
const root = config.apiBaseUrl.replace(/\/+$/, '');
|
|
42
|
+
return `${root}${path.startsWith('/v1.0/') ? path : `/v1.0${path}`}`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Whether a failed `ctx.http` call was the caller's cancellation rather than a transport failure.
|
|
46
|
+
* @param cause - the value `ctx.http` rejected with.
|
|
47
|
+
* @param signal - the call's signal.
|
|
48
|
+
* @returns true when the caller cancelled the request.
|
|
49
|
+
*/
|
|
50
|
+
function isCancellation(cause, signal) {
|
|
51
|
+
if (signal?.aborted === true)
|
|
52
|
+
return true;
|
|
53
|
+
if ((cause instanceof Error || cause instanceof DOMException) &&
|
|
54
|
+
cause.name === 'AbortError') {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return signal?.reason !== undefined && cause === signal.reason;
|
|
58
|
+
}
|
|
59
|
+
/** A cancelled request rejects with the signal's reason, so the classifier sees the cancellation itself. */
|
|
60
|
+
function cancellation(cause, signal) {
|
|
61
|
+
return signal?.aborted === true ? signal.reason : cause;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The access token of one request, or undefined for the endpoints that mint it.
|
|
65
|
+
* @param bot - the adapter whose token cache the call uses.
|
|
66
|
+
* @param base - which root the request uses.
|
|
67
|
+
* @param path - the endpoint path.
|
|
68
|
+
* @returns the token to carry, if the endpoint needs one.
|
|
69
|
+
*/
|
|
70
|
+
async function authorization(bot, base, path) {
|
|
71
|
+
if (base === 'api') {
|
|
72
|
+
// The version root may or may not be part of the path the caller handed over.
|
|
73
|
+
const endpoint = path.startsWith('/v1.0/')
|
|
74
|
+
? path.slice('/v1.0'.length)
|
|
75
|
+
: path;
|
|
76
|
+
if (CREDENTIAL_PATHS.has(endpoint))
|
|
77
|
+
return undefined;
|
|
78
|
+
// The request that mints the token must not wait for the token it is minting.
|
|
79
|
+
if (bot.tokens.minting)
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
return await bot.tokens.get();
|
|
83
|
+
}
|
|
84
|
+
/** Send one request and unwrap its answer. */
|
|
85
|
+
async function send(bot, base, method, path, config, token) {
|
|
86
|
+
const url = endpointOf(bot.config, base, path);
|
|
87
|
+
const params = { ...config.params };
|
|
88
|
+
if (token !== undefined && base === 'oapi')
|
|
89
|
+
params.access_token = token;
|
|
90
|
+
const headers = {};
|
|
91
|
+
if (token !== undefined && base === 'api') {
|
|
92
|
+
headers['x-acs-dingtalk-access-token'] = token;
|
|
93
|
+
}
|
|
94
|
+
let response;
|
|
95
|
+
try {
|
|
96
|
+
response = await bot.ctx.http(url, {
|
|
97
|
+
method,
|
|
98
|
+
...(config.data === undefined ? {} : { data: config.data }),
|
|
99
|
+
...(Object.keys(params).length === 0 ? {} : { params }),
|
|
100
|
+
...(Object.keys(headers).length === 0 ? {} : { headers }),
|
|
101
|
+
...(config.signal === undefined ? {} : { signal: config.signal }),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
catch (cause) {
|
|
105
|
+
if (isCancellation(cause, config.signal)) {
|
|
106
|
+
throw cancellation(cause, config.signal);
|
|
107
|
+
}
|
|
108
|
+
throw new DingtalkApiError(`dingtalk: ${method} ${path} request failed: ${messageOf(cause)}`, { cause });
|
|
109
|
+
}
|
|
110
|
+
let text;
|
|
111
|
+
try {
|
|
112
|
+
text = await response.text();
|
|
113
|
+
}
|
|
114
|
+
catch (cause) {
|
|
115
|
+
// Reading the body fails when the caller cancelled mid-answer; that is still a cancellation.
|
|
116
|
+
if (isCancellation(cause, config.signal)) {
|
|
117
|
+
throw cancellation(cause, config.signal);
|
|
118
|
+
}
|
|
119
|
+
throw new DingtalkApiError(`dingtalk: ${method} ${path} answered ${response.status} but its body could not be read`, { status: response.status, cause });
|
|
120
|
+
}
|
|
121
|
+
let body;
|
|
122
|
+
try {
|
|
123
|
+
body = text === '' ? undefined : JSON.parse(text);
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
// The IP-dimension rate limiter answers an HTML page, not JSON; a gateway error page looks the same.
|
|
127
|
+
throw new DingtalkApiError(`dingtalk: ${method} ${path} answered ${response.status} without JSON: ${text.slice(0, BODY_PREFIX_BYTES)}`, {
|
|
128
|
+
status: response.status,
|
|
129
|
+
nonJson: true,
|
|
130
|
+
bodyText: text.slice(0, BODY_PREFIX_BYTES),
|
|
131
|
+
cause: error,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
const record = asRecord(body);
|
|
135
|
+
const code = record?.code ?? record?.errcode;
|
|
136
|
+
const message = record?.message ?? record?.errmsg;
|
|
137
|
+
const details = {
|
|
138
|
+
status: response.status,
|
|
139
|
+
...(typeof code === 'string' || typeof code === 'number' ? { code } : {}),
|
|
140
|
+
...(typeof record?.requestid === 'string'
|
|
141
|
+
? { requestid: record.requestid }
|
|
142
|
+
: {}),
|
|
143
|
+
...(text === '' ? {} : { bodyText: text.slice(0, BODY_PREFIX_BYTES) }),
|
|
144
|
+
};
|
|
145
|
+
const suffix = (code === undefined ? '' : ` (${String(code)})`) +
|
|
146
|
+
(typeof message === 'string' ? `: ${message}` : '');
|
|
147
|
+
if (response.status < 200 || response.status >= 300) {
|
|
148
|
+
throw new DingtalkApiError(`dingtalk: ${method} ${path} answered ${response.status}${suffix}`, details);
|
|
149
|
+
}
|
|
150
|
+
if (code !== undefined && code !== 0) {
|
|
151
|
+
// The legacy `oapi` endpoints answer HTTP 200 with an `errcode`; reading that as a success would lose the refusal.
|
|
152
|
+
throw new DingtalkApiError(`dingtalk: ${method} ${path} answered ${response.status}${suffix}`, details);
|
|
153
|
+
}
|
|
154
|
+
return body;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Call one DingTalk endpoint.
|
|
158
|
+
*
|
|
159
|
+
* A call refused for its access token re-mints the token and is retried once; every other failure is thrown as the
|
|
160
|
+
* `DingtalkApiError` `classify` maps. A cancelled call rejects with the cancellation itself.
|
|
161
|
+
*
|
|
162
|
+
* @param bot - the adapter whose configuration and token cache the call uses.
|
|
163
|
+
* @param base - which root (`/v1.0` OpenAPI or the legacy `oapi`) the path belongs to.
|
|
164
|
+
* @param method - HTTP method.
|
|
165
|
+
* @param path - endpoint path, with or without the `/v1.0` segment.
|
|
166
|
+
* @param config - body, query parameters and cancellation of the call.
|
|
167
|
+
* @returns the unwrapped JSON answer.
|
|
168
|
+
*/
|
|
169
|
+
export async function invoke(bot, base, method, path, config = {}) {
|
|
170
|
+
const token = await authorization(bot, base, path);
|
|
171
|
+
try {
|
|
172
|
+
return await send(bot, base, method, path, config, token);
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
if (!isAuthFailure(error) || token === undefined)
|
|
176
|
+
throw error;
|
|
177
|
+
bot.tokens.invalidate();
|
|
178
|
+
const fresh = await authorization(bot, base, path);
|
|
179
|
+
return await send(bot, base, method, path, config, fresh);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAgC7D;;;GAGG;AACH,MAAM,gBAAgB,GAAwB,IAAI,GAAG,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAA;AAE9E,wGAAwG;AACxG,MAAM,iBAAiB,GAAG,GAAG,CAAA;AAE7B,yDAAyD;AACzD,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACzE,CAA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