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 CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ## 联系我
8
8
 
9
- **如你有业务需要,可以点此[联系我](https://f.kdocs.cn/g/SIdEy3GE/),三个工作日内必定回复。**
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,远程调用wpsjsapi
17
+ 用于学习wpsjsapi,远程调用wpsjs-api
18
18
 
19
- 使用wpsjs-1.0api。支持异步任务。支持`KSDrive`的类型。
19
+ 使用wpsjs-api。支持异步任务。
20
20
 
21
21
  在远程部署代码好代码,通过传输的调用信息进行调用api。原理是使用代理收集调用信息,之后让远程代码进行调用并且返回结果。
22
22
 
23
- 远程代码在`remoteCode`目录的`index.js`文件里面。将其复制粘贴到对应`webhook`的脚本中才能支持`wpsjsGlobal`的调用。
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
  ![脚本令牌](https://qn.cache.wpscdn.cn/asdocs/assets/apitoken.Dv10SGDp.png "脚本令牌")
33
34
 
34
35
  ### webhook链接
36
+
37
+ **!!!webhook链接的脚本内容必须部署为`tadcode-wpsjs/remoteCode/index.js`中的脚本内容。**
38
+
35
39
  ![webhook链接](https://qn.cache.wpscdn.cn/asdocs/assets/webhook.DnCJD55j.png "webhook链接")
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 { RemoteCallWpsjsGlobal } from 'tadcode-wpsjs'
41
- const remoteCall = new RemoteCallWpsjsGlobal()
42
- remoteCall.scriptToken = 'xxxx' /* 脚本令牌 */
43
- remoteCall.webhookURL = 'xxxx' /* webhook url地址 */
44
- /* 获取表信息 */
45
- const sheets = await remoteCall.wpsjsGlobal.Application.Sheet.GetSheets().execution()
46
- const data = sheets.map(({ id, name, sheetType }) => ({ id, name, sheetType }))
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 V from"path";var X="execution";class W{sync=!0;scriptToken="";webhookURL="";asyncQueryURL="https://www.kdocs.cn/api/v3/script/task";printLog=!1;writeLogFile=!1;info=[];wpsjsGlobal=new Proxy(()=>{},{get:(G,j,q)=>{return this.info.push({method:"get",arguments:[j]}),q},apply:(G,j,q)=>{let{info:z}=this;if(z.at(-1)?.arguments[0]===X)return this.execute().then(async(B)=>{let{printLog:H,writeLogFile:M}=this,E=await B.json();if(H)Y(E);if(M)Bun.write("./wpsjs.log.json",JSON.stringify(E,void 0,4));return E.data.result});return z.push({method:"apply",arguments:q}),j}});queryTask(G){let{asyncQueryURL:j}=this,q=new URL(j);return q.searchParams.append("task_id",G),fetch(q.toString())}execute(){let{scriptToken:G,webhookURL:j,sync:q,info:z}=this;z.pop();let K=JSON.stringify({Context:{argv:{info:z}}});this.info=[];let B=V.parse(j);B.base=q?"sync_task":"task";let H=V.format(B);return fetch(H,{method:"post",headers:{"Content-Type":"application/json","AirScript-Token":G},body:K})}}var Y=(G)=>{let j=G,{status:q,data:z,error:K,error_details:B}=j;console.log("status:",q);let H=z.logs.at(-1).unix_time-z.logs[1].unix_time;if(console.log("runtime:",H),K)console.error(B.name),console.error(B.msg),console.error(B.stack);let M=z.logs.filter((E)=>E.filename!=="<system>");if(M.length!==0){console.log("log:");for(let E of M)console[E.level](...E.args)}return console.log("result:"),console.log(z.result),j};export{W as default};
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
- const fnName = 'execution'
4
- export default class RemoteCallWpsjsGlobal {
5
- /* true 同步执行代码 */
6
- sync = true
7
- scriptToken = ''
8
- webhookURL = ''
9
- asyncQueryURL = 'https://www.kdocs.cn/api/v3/script/task'
10
- /* true 在控制台上打印日志 */
11
- printLog = false
12
- /* true 在当前目录下,写入日志文件 */
13
- writeLogFile = false
14
- info: InfoItem[] = []
15
- wpsjsGlobal = new Proxy(() => { }, {
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] === fnName) {
24
- return this.execute().then(async (response) => {
25
- const { printLog, writeLogFile } = this
26
- const responseJSON = await response.json()
27
- if (printLog) printInfo(responseJSON)
28
- if (writeLogFile) Bun.write('./wpsjs.log.json', JSON.stringify((responseJSON), undefined, 4))
29
- return responseJSON['data']['result']
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tadcode-wpsjs",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -13,7 +13,6 @@ for (const item of info) {
13
13
  } break
14
14
  default:
15
15
  throw Error('reflect method undefined')
16
- break
17
16
  }
18
17
  }
19
18
  return current
package/types/index.d.ts CHANGED
@@ -10,7 +10,7 @@ interface WpsjsGlobal {
10
10
  type WithExecutionWpsjsGlobal = WithExecution<WpsjsGlobal>
11
11
  interface InfoItem {
12
12
  method: keyof typeof Reflect
13
- arguments: (string | symbol)[]
13
+ arguments: string[]
14
14
  }
15
15
  interface RemoteCallWpsjsGlobalReturn {
16
16
  "data": {