tadcode-wpsjs 0.1.2 → 0.1.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.
- package/README.md +30 -11
- package/index.js +1 -1
- package/index.ts +53 -51
- package/package.json +1 -1
- package/remoteCode/index.js +0 -1
- package/types/index.d.ts +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
## 联系我
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
**如你有业务需要wps代码服务,可以点此[联系我](https://f.kdocs.cn/g/SIdEy3GE/),三个工作日内必定回复。**
|
|
10
10
|
|
|
11
11
|
**如您公司存在敏感信息业务,需要长期人才,亦可来讯商谈。**
|
|
12
12
|
|
|
@@ -14,13 +14,13 @@
|
|
|
14
14
|
|
|
15
15
|
## 应用
|
|
16
16
|
|
|
17
|
-
用于学习wpsjsapi,远程调用
|
|
17
|
+
用于学习wpsjsapi,远程调用wpsjs-api。
|
|
18
18
|
|
|
19
|
-
使用wpsjs-
|
|
19
|
+
使用wpsjs-api。支持异步任务。
|
|
20
20
|
|
|
21
21
|
在远程部署代码好代码,通过传输的调用信息进行调用api。原理是使用代理收集调用信息,之后让远程代码进行调用并且返回结果。
|
|
22
22
|
|
|
23
|
-
远程代码在`remoteCode`目录的`index.js
|
|
23
|
+
远程代码在`tadcode-wpsjs/remoteCode`目录的`index.js`文件中。将其复制粘贴到对应`webhook`的脚本中才能支持`wpsjsGlobal`的调用。
|
|
24
24
|
|
|
25
25
|
## 安装
|
|
26
26
|
|
|
@@ -29,21 +29,40 @@
|
|
|
29
29
|
## 获取脚本令牌和webhook链接
|
|
30
30
|
|
|
31
31
|
### 获取脚本令牌
|
|
32
|
+
|
|
32
33
|

|
|
33
34
|
|
|
34
35
|
### webhook链接
|
|
36
|
+
|
|
37
|
+
**!!!webhook链接的脚本内容必须部署为`tadcode-wpsjs/remoteCode/index.js`中的脚本内容。**
|
|
38
|
+
|
|
35
39
|

|
|
36
40
|
|
|
37
41
|
## 案例
|
|
38
42
|
|
|
43
|
+
### 获取当前多维表的所有表格信息
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { initRemoteCallWpsjsGlobal } from 'tadcode-wpsjs'
|
|
47
|
+
/* 脚本令牌 具体获取方式参考上面 */
|
|
48
|
+
const scriptToken = 'xxxx'
|
|
49
|
+
/* webhook url地址 具体获取方式参考上面 */
|
|
50
|
+
const webhookURL = 'xxxx'
|
|
51
|
+
const wpsjsGlobal = initRemoteCallWpsjsGlobal({ scriptToken, webhookURL, })
|
|
52
|
+
const data = await wpsjsGlobal.Application.Sheet.GetSheets().execution()
|
|
53
|
+
console.log(data)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 执行脚本并且返回(不用部署remoteCode代码)
|
|
57
|
+
|
|
39
58
|
```ts
|
|
40
|
-
import {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
const data =
|
|
59
|
+
import { execution } from 'tadcode-wpsjs'
|
|
60
|
+
/* 脚本令牌 具体获取方式参考上面 */
|
|
61
|
+
const scriptToken = 'xxxx'
|
|
62
|
+
/* webhook url地址 具体获取方式参考上面 */
|
|
63
|
+
const webhookURL = 'xxxx'
|
|
64
|
+
const result=await execution({ scriptToken, webhookURL })
|
|
65
|
+
const data =await result.json()
|
|
47
66
|
console.log(data)
|
|
48
67
|
```
|
|
49
68
|
|
package/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
import
|
|
2
|
+
import{writeFile as $}from"fs/promises";import Y from"path";var O=(B)=>{let{scriptToken:E,webhookURL:M,isAsync:H=!1,printLog:N=!1,writeLogFile:J=!1}=B,z=[];return new Proxy(()=>{},{get:(K,S,V)=>{return z.push({method:"get",arguments:[S]}),V},apply:(K,S,V)=>{if(z.at(-1)?.arguments[0]==="execution")return z.pop(),q({scriptToken:E,webhookURL:M,isAsync:H,argv:{info:z}}).then(async(Z)=>{let X=await Z.json();if(z=[],N)G(X);if(J)W(X);return X.data.result});return z.push({method:"apply",arguments:V}),S}})},q=({scriptToken:B,webhookURL:E,isAsync:M,argv:H})=>{let N=JSON.stringify({Context:{argv:H}}),J=Y.parse(E);J.base=M?"task":"sync_task";let z=Y.format(J);return fetch(z,{method:"post",headers:{"Content-Type":"application/json","AirScript-Token":B},body:N})},U=(B)=>{let E=new URL("https://www.kdocs.cn/api/v3/script/task");return E.searchParams.append("task_id",B),fetch(E)},G=(B)=>{let E=B,{status:M,data:H,error:N,error_details:J}=E;console.log("status:",M);let z=H.logs.at(-1).unix_time-H.logs[1].unix_time;if(console.log("runtime:",z),N)console.error(J.name),console.error(J.msg),console.error(J.stack);let Q=H.logs.filter((K)=>K.filename!=="<system>");if(Q.length!==0){console.log("log:");for(let K of Q)console[K.level](...K.args)}return console.log("result:"),console.log(H.result),E},W=(B)=>{$("./wpsjs.log.json",JSON.stringify(B,void 0,4))};export{W as writeLogFileToCurrent,U as queryTask,O as initRemoteCallWpsjsGlobal,q as execution};
|
package/index.ts
CHANGED
|
@@ -1,65 +1,61 @@
|
|
|
1
1
|
import type { InfoItem, RemoteCallWpsjsGlobalReturn, WithExecutionWpsjsGlobal } from './types'
|
|
2
|
+
import { writeFile } from 'fs/promises'
|
|
2
3
|
import path from 'path'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
printLog = false
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
get: (_target, p, receiver) => {
|
|
17
|
-
this.info.push({ method: 'get', arguments: [p] })
|
|
4
|
+
interface Config {
|
|
5
|
+
scriptToken: string
|
|
6
|
+
webhookURL: string
|
|
7
|
+
isAsync?: boolean
|
|
8
|
+
printLog?: boolean
|
|
9
|
+
writeLogFile?: boolean
|
|
10
|
+
}
|
|
11
|
+
const initRemoteCallWpsjsGlobal = (config: Config) => {
|
|
12
|
+
const { scriptToken, webhookURL, isAsync = false, printLog = false, writeLogFile = false, } = config
|
|
13
|
+
let info: InfoItem[] = []
|
|
14
|
+
const handle: ProxyHandler<() => void> = {
|
|
15
|
+
get: (_target, p: string, receiver) => {
|
|
16
|
+
info.push({ method: 'get', arguments: [p] })
|
|
18
17
|
return receiver
|
|
19
18
|
},
|
|
20
19
|
apply: (_target, thisArg, argArray) => {
|
|
21
|
-
const { info } = this
|
|
22
20
|
const lastInfo = info.at(-1)
|
|
23
|
-
if (lastInfo?.arguments[0] ===
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
21
|
+
if (lastInfo?.arguments[0] === 'execution') {
|
|
22
|
+
info.pop() /* 删除 execution的调用信息 */
|
|
23
|
+
return execution({ scriptToken, webhookURL, isAsync, argv: { info } })
|
|
24
|
+
.then(async (response) => {
|
|
25
|
+
const responseObj = await response.json()
|
|
26
|
+
info = [] /* 执行结束,清空调用 */
|
|
27
|
+
if (printLog) printInfo(responseObj)
|
|
28
|
+
if (writeLogFile) writeLogFileToCurrent(responseObj)
|
|
29
|
+
return responseObj['data']['result']
|
|
30
|
+
})
|
|
31
31
|
}
|
|
32
32
|
info.push({ method: 'apply', arguments: argArray })
|
|
33
33
|
return thisArg
|
|
34
34
|
},
|
|
35
|
-
}) as unknown as WithExecutionWpsjsGlobal
|
|
36
|
-
queryTask(id: string) {
|
|
37
|
-
const { asyncQueryURL } = this
|
|
38
|
-
const url = new URL(asyncQueryURL)
|
|
39
|
-
url.searchParams.append('task_id', id)
|
|
40
|
-
return fetch(url.toString())
|
|
41
|
-
}
|
|
42
|
-
execute() {
|
|
43
|
-
const { scriptToken, webhookURL, sync, info } = this
|
|
44
|
-
/* 删除 execution的调用信息 */
|
|
45
|
-
info.pop()
|
|
46
|
-
/* 获取请求信息 */
|
|
47
|
-
const data = JSON.stringify({ Context: { argv: { info }, } })
|
|
48
|
-
/* 清空调用 */
|
|
49
|
-
this.info = []
|
|
50
|
-
/* 根据是否同步返回不同的url */
|
|
51
|
-
const parsedPath = path.parse(webhookURL)
|
|
52
|
-
parsedPath.base = sync ? 'sync_task' : 'task'
|
|
53
|
-
const url = path.format(parsedPath)
|
|
54
|
-
return fetch(url, {
|
|
55
|
-
method: 'post',
|
|
56
|
-
headers: {
|
|
57
|
-
'Content-Type': 'application/json',
|
|
58
|
-
'AirScript-Token': scriptToken
|
|
59
|
-
},
|
|
60
|
-
body: data
|
|
61
|
-
})
|
|
62
35
|
}
|
|
36
|
+
return new Proxy(() => { }, handle) as unknown as WithExecutionWpsjsGlobal
|
|
37
|
+
}
|
|
38
|
+
type ExecutionConfig = Pick<Config, 'scriptToken' | 'webhookURL' | 'isAsync'> & { argv?: Record<string | number, any> }
|
|
39
|
+
const execution = ({ scriptToken, webhookURL, isAsync, argv }: ExecutionConfig) => {
|
|
40
|
+
/* 获取请求信息 */
|
|
41
|
+
const data = JSON.stringify({ Context: { argv, } })
|
|
42
|
+
/* 根据是否同步返回不同的url */
|
|
43
|
+
const parsedPath = path.parse(webhookURL)
|
|
44
|
+
parsedPath.base = isAsync ? 'task' : 'sync_task'
|
|
45
|
+
const url = path.format(parsedPath)
|
|
46
|
+
return fetch(url, {
|
|
47
|
+
method: 'post',
|
|
48
|
+
headers: {
|
|
49
|
+
'Content-Type': 'application/json',
|
|
50
|
+
'AirScript-Token': scriptToken
|
|
51
|
+
},
|
|
52
|
+
body: data
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
const queryTask = (id: string) => {
|
|
56
|
+
const url = new URL('https://www.kdocs.cn/api/v3/script/task')
|
|
57
|
+
url.searchParams.append('task_id', id)
|
|
58
|
+
return fetch(url)
|
|
63
59
|
}
|
|
64
60
|
const printInfo = (remoteCallResponse: RemoteCallWpsjsGlobalReturn) => {
|
|
65
61
|
const responseData = remoteCallResponse as RemoteCallWpsjsGlobalReturn
|
|
@@ -83,3 +79,9 @@ const printInfo = (remoteCallResponse: RemoteCallWpsjsGlobalReturn) => {
|
|
|
83
79
|
console.log(data.result)
|
|
84
80
|
return responseData
|
|
85
81
|
}
|
|
82
|
+
const writeLogFileToCurrent = (data: object) => {
|
|
83
|
+
writeFile('./wpsjs.log.json', JSON.stringify((data), undefined, 4))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export { execution, initRemoteCallWpsjsGlobal, queryTask, writeLogFileToCurrent }
|
|
87
|
+
export type * from './types'
|
package/package.json
CHANGED
package/remoteCode/index.js
CHANGED
package/types/index.d.ts
CHANGED