tadcode-wpsjs 0.3.0 → 0.3.2
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/index.js +1 -1
- package/index.ts +110 -110
- package/package.json +15 -3
- package/types/DB_Application_Record.d.ts +2 -2
- package/types/KSDrive.d.ts +5 -5
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
import{writeFile as G}from"fs/promises";import $ 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,Q,S)=>{return z.push({method:"get",arguments:[Q]}),S},set:(K,Q,S,Z)=>{return z.push({method:"set",arguments:[Q,S]}),Z},apply:(K,Q,S)=>{if(z.at(-1)?.arguments[0]==="execution")return z.pop(),W({scriptToken:E,webhookURL:M,isAsync:H,argv:{info:z}}).then(async(q)=>{let Y=await q.json();if(z=[],N)D(Y);if(J)await P(Y);return Y.data.result});return z.push({method:"apply",arguments:S}),Q}})},W=({scriptToken:B,webhookURL:E,isAsync:M,argv:H})=>{let N=JSON.stringify({Context:{argv:H}}),J=$.parse(E);J.base=M?"task":"sync_task";let z=$.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)},D=(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 X=H.logs.filter((K)=>K.filename!=="<system>");if(X.length!==0){console.log("log:");for(let K of X)console[K.level](...K.args)}return console.log("result:"),console.log(H.result),E},P=(B)=>{return G("./wpsjs.log.json",JSON.stringify(B,void 0,2))};export{P as writeLogFileToCurrent,U as queryTask,O as initRemoteCallWpsjsGlobal,W as execution};
|
package/index.ts
CHANGED
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
import type { InfoItem, RemoteCallWpsjsGlobalReturn, WithExecutionWpsjsGlobal } from './types'
|
|
2
|
-
import { writeFile } from 'fs/promises'
|
|
3
|
-
import path from 'path'
|
|
4
|
-
|
|
5
|
-
interface Config {
|
|
6
|
-
/**脚本令牌 */
|
|
7
|
-
scriptToken: string
|
|
8
|
-
/**脚本链接 */
|
|
9
|
-
webhookURL: string
|
|
10
|
-
/**异步任务 */
|
|
11
|
-
isAsync?: boolean
|
|
12
|
-
/**在控制台打印日志 */
|
|
13
|
-
printLog?: boolean
|
|
14
|
-
/**在当前路径下写入日志文件 */
|
|
15
|
-
writeLogFile?: boolean
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const initRemoteCallWpsjsGlobal = (config: Config) => {
|
|
19
|
-
const { scriptToken, webhookURL, isAsync = false, printLog = false, writeLogFile = false, } = config
|
|
20
|
-
let info: InfoItem[] = []
|
|
21
|
-
const handle: ProxyHandler<() => void> = {
|
|
22
|
-
get: (_target, p: string, receiver) => {
|
|
23
|
-
info.push({ method: 'get', arguments: [p] })
|
|
24
|
-
return receiver
|
|
25
|
-
},
|
|
26
|
-
set: (_target, p: string, newValue, receiver) => {
|
|
27
|
-
info.push({ method: 'set', arguments: [p, newValue] })
|
|
28
|
-
return receiver
|
|
29
|
-
},
|
|
30
|
-
apply: (_target, thisArg, argArray) => {
|
|
31
|
-
const lastInfo = info.at(-1)
|
|
32
|
-
if (lastInfo?.arguments[0] === 'execution') {
|
|
33
|
-
info.pop() /* 删除 execution的调用信息 */
|
|
34
|
-
return execution({ scriptToken, webhookURL, isAsync, argv: { info } })
|
|
35
|
-
.then(async (response) => {
|
|
36
|
-
const responseObj = await response.json()
|
|
37
|
-
info = [] /* 执行结束,清空调用 */
|
|
38
|
-
if (printLog) printInfo(responseObj)
|
|
39
|
-
|
|
40
|
-
if (writeLogFile) await writeLogFileToCurrent(responseObj)
|
|
41
|
-
return responseObj['data']['result']
|
|
42
|
-
})
|
|
43
|
-
}
|
|
44
|
-
info.push({ method: 'apply', arguments: argArray })
|
|
45
|
-
return thisArg
|
|
46
|
-
},
|
|
47
|
-
}
|
|
48
|
-
return new Proxy(() => { }, handle) as unknown as WithExecutionWpsjsGlobal
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
type ExecutionConfig = Pick<Config, 'scriptToken' | 'webhookURL' | 'isAsync'> & { argv?: Record<string | number, any> }
|
|
52
|
-
|
|
53
|
-
const execution = ({ scriptToken, webhookURL, isAsync, argv }: ExecutionConfig) => {
|
|
54
|
-
/* 获取请求信息 */
|
|
55
|
-
const data = JSON.stringify({ Context: { argv, } })
|
|
56
|
-
/* 根据是否同步返回不同的url */
|
|
57
|
-
const parsedPath = path.parse(webhookURL)
|
|
58
|
-
parsedPath.base = isAsync ? 'task' : 'sync_task'
|
|
59
|
-
const url = path.format(parsedPath)
|
|
60
|
-
return fetch(url, {
|
|
61
|
-
method: 'post',
|
|
62
|
-
headers: {
|
|
63
|
-
'Content-Type': 'application/json',
|
|
64
|
-
'AirScript-Token': scriptToken
|
|
65
|
-
},
|
|
66
|
-
body: data
|
|
67
|
-
})
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const queryTask = (id: string) => {
|
|
71
|
-
const url = new URL('https://www.kdocs.cn/api/v3/script/task')
|
|
72
|
-
url.searchParams.append('task_id', id)
|
|
73
|
-
return fetch(url)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const printInfo = (remoteCallResponse: RemoteCallWpsjsGlobalReturn) => {
|
|
77
|
-
const responseData = remoteCallResponse as RemoteCallWpsjsGlobalReturn
|
|
78
|
-
const { status, data, error, error_details } = responseData
|
|
79
|
-
console.log('status:', status)
|
|
80
|
-
const time = data.logs.at(-1)!.unix_time - data.logs[1].unix_time
|
|
81
|
-
console.log('runtime:', time)
|
|
82
|
-
if (error) {
|
|
83
|
-
console.error(error_details.name)
|
|
84
|
-
console.error(error_details.msg)
|
|
85
|
-
console.error(error_details.stack)
|
|
86
|
-
}
|
|
87
|
-
const userInfo = data.logs.filter(x => x.filename !== '<system>')
|
|
88
|
-
if (userInfo.length !== 0) {
|
|
89
|
-
console.log('log:')
|
|
90
|
-
for (const item of userInfo) {
|
|
91
|
-
console[item.level](...item.args)
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
console.log('result:')
|
|
95
|
-
console.log(data.result)
|
|
96
|
-
return responseData
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const writeLogFileToCurrent = (data: object) => {
|
|
100
|
-
return writeFile('./wpsjs.log.json', JSON.stringify((data), undefined, 2))
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export type * from './types'
|
|
104
|
-
|
|
105
|
-
export {
|
|
106
|
-
execution,
|
|
107
|
-
initRemoteCallWpsjsGlobal,
|
|
108
|
-
queryTask,
|
|
109
|
-
writeLogFileToCurrent,
|
|
110
|
-
}
|
|
1
|
+
import type { InfoItem, RemoteCallWpsjsGlobalReturn, WithExecutionWpsjsGlobal } from './types'
|
|
2
|
+
import { writeFile } from 'fs/promises'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
|
|
5
|
+
interface Config {
|
|
6
|
+
/**脚本令牌 */
|
|
7
|
+
scriptToken: string
|
|
8
|
+
/**脚本链接 */
|
|
9
|
+
webhookURL: string
|
|
10
|
+
/**异步任务 */
|
|
11
|
+
isAsync?: boolean
|
|
12
|
+
/**在控制台打印日志 */
|
|
13
|
+
printLog?: boolean
|
|
14
|
+
/**在当前路径下写入日志文件 */
|
|
15
|
+
writeLogFile?: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const initRemoteCallWpsjsGlobal = (config: Config) => {
|
|
19
|
+
const { scriptToken, webhookURL, isAsync = false, printLog = false, writeLogFile = false, } = config
|
|
20
|
+
let info: InfoItem[] = []
|
|
21
|
+
const handle: ProxyHandler<() => void> = {
|
|
22
|
+
get: (_target, p: string, receiver) => {
|
|
23
|
+
info.push({ method: 'get', arguments: [p] })
|
|
24
|
+
return receiver
|
|
25
|
+
},
|
|
26
|
+
set: (_target, p: string, newValue, receiver) => {
|
|
27
|
+
info.push({ method: 'set', arguments: [p, newValue] })
|
|
28
|
+
return receiver
|
|
29
|
+
},
|
|
30
|
+
apply: (_target, thisArg, argArray) => {
|
|
31
|
+
const lastInfo = info.at(-1)
|
|
32
|
+
if (lastInfo?.arguments[0] === 'execution') {
|
|
33
|
+
info.pop() /* 删除 execution的调用信息 */
|
|
34
|
+
return execution({ scriptToken, webhookURL, isAsync, argv: { info } })
|
|
35
|
+
.then(async (response) => {
|
|
36
|
+
const responseObj = await response.json()
|
|
37
|
+
info = [] /* 执行结束,清空调用 */
|
|
38
|
+
if (printLog) printInfo(responseObj)
|
|
39
|
+
|
|
40
|
+
if (writeLogFile) await writeLogFileToCurrent(responseObj)
|
|
41
|
+
return responseObj['data']['result']
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
info.push({ method: 'apply', arguments: argArray })
|
|
45
|
+
return thisArg
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
return new Proxy(() => { }, handle) as unknown as WithExecutionWpsjsGlobal
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type ExecutionConfig = Pick<Config, 'scriptToken' | 'webhookURL' | 'isAsync'> & { argv?: Record<string | number, any> }
|
|
52
|
+
|
|
53
|
+
const execution = ({ scriptToken, webhookURL, isAsync, argv }: ExecutionConfig) => {
|
|
54
|
+
/* 获取请求信息 */
|
|
55
|
+
const data = JSON.stringify({ Context: { argv, } })
|
|
56
|
+
/* 根据是否同步返回不同的url */
|
|
57
|
+
const parsedPath = path.parse(webhookURL)
|
|
58
|
+
parsedPath.base = isAsync ? 'task' : 'sync_task'
|
|
59
|
+
const url = path.format(parsedPath)
|
|
60
|
+
return fetch(url, {
|
|
61
|
+
method: 'post',
|
|
62
|
+
headers: {
|
|
63
|
+
'Content-Type': 'application/json',
|
|
64
|
+
'AirScript-Token': scriptToken
|
|
65
|
+
},
|
|
66
|
+
body: data
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const queryTask = (id: string) => {
|
|
71
|
+
const url = new URL('https://www.kdocs.cn/api/v3/script/task')
|
|
72
|
+
url.searchParams.append('task_id', id)
|
|
73
|
+
return fetch(url)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const printInfo = (remoteCallResponse: RemoteCallWpsjsGlobalReturn) => {
|
|
77
|
+
const responseData = remoteCallResponse as RemoteCallWpsjsGlobalReturn
|
|
78
|
+
const { status, data, error, error_details } = responseData
|
|
79
|
+
console.log('status:', status)
|
|
80
|
+
const time = data.logs.at(-1)!.unix_time - data.logs[1].unix_time
|
|
81
|
+
console.log('runtime:', time)
|
|
82
|
+
if (error) {
|
|
83
|
+
console.error(error_details.name)
|
|
84
|
+
console.error(error_details.msg)
|
|
85
|
+
console.error(error_details.stack)
|
|
86
|
+
}
|
|
87
|
+
const userInfo = data.logs.filter(x => x.filename !== '<system>')
|
|
88
|
+
if (userInfo.length !== 0) {
|
|
89
|
+
console.log('log:')
|
|
90
|
+
for (const item of userInfo) {
|
|
91
|
+
console[item.level](...item.args)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
console.log('result:')
|
|
95
|
+
console.log(data.result)
|
|
96
|
+
return responseData
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const writeLogFileToCurrent = (data: object) => {
|
|
100
|
+
return writeFile('./wpsjs.log.json', JSON.stringify((data), undefined, 2))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type * from './types'
|
|
104
|
+
|
|
105
|
+
export {
|
|
106
|
+
execution,
|
|
107
|
+
initRemoteCallWpsjsGlobal,
|
|
108
|
+
queryTask,
|
|
109
|
+
writeLogFileToCurrent,
|
|
110
|
+
}
|
package/package.json
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tadcode-wpsjs",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"hot": "NODE_ENV=development bun --hot ignore/index.ts",
|
|
8
8
|
"dev": "NODE_ENV=development bun --watch index.ts",
|
|
9
|
-
"build": "NODE_ENV=production bun build index.ts --minify --outfile index.js"
|
|
9
|
+
"build": "NODE_ENV=production bun build index.ts --target node --minify --outfile index.js"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"@types/bun": "latest"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|
|
15
15
|
"tadcode-wpsjs"
|
|
16
|
-
]
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": "./index.js",
|
|
20
|
+
"types": "./index.ts"
|
|
21
|
+
},
|
|
22
|
+
"./types": {
|
|
23
|
+
"types": "./types/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./types/DB_Application": {
|
|
26
|
+
"types": "./types/DB_Application.d.ts"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
17
29
|
}
|
|
@@ -166,11 +166,11 @@ interface RecordFuncOptions {
|
|
|
166
166
|
Filter: RecordsFuncOptionsFilter
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
export type { Operator, DynamicDateRange, RecordItem, RecordFuncOptions }
|
|
169
|
+
export type { Operator, DynamicDateRange, RecordItem, RecordFuncOptions, RecordsFuncOptionsFilter }
|
|
170
170
|
|
|
171
171
|
/* 行记录 */
|
|
172
172
|
export default interface DBRecord {
|
|
173
|
-
CreateRecords(options: Pick<RecordFuncOptions, 'SheetId'> & { Records:
|
|
173
|
+
CreateRecords(options: Pick<RecordFuncOptions, 'SheetId'> & { Records: { fields: Record<string, any> }[] }): RecordItem[]
|
|
174
174
|
GetRecord(options: Pick<RecordFuncOptions, 'SheetId' | 'RecordId'>): RecordItem
|
|
175
175
|
GetRecords(options: Pick<RecordFuncOptions, 'SheetId'> & Partial<Pick<RecordFuncOptions, 'ViewId' | 'PageSize' | 'Offset' | 'MaxRecords' | 'Fields' | 'Filter'>>): { records: RecordItem[] }
|
|
176
176
|
UpdateRecords(options: Pick<RecordFuncOptions, 'SheetId' | 'Records'>): RecordItem[]
|
package/types/KSDrive.d.ts
CHANGED
|
@@ -52,9 +52,9 @@ export default interface KSDrive {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
listFiles(options?: {
|
|
55
|
-
dirUrl
|
|
56
|
-
offset
|
|
57
|
-
count
|
|
58
|
-
includeExts
|
|
59
|
-
})
|
|
55
|
+
dirUrl?: string
|
|
56
|
+
offset?: number
|
|
57
|
+
count?: number
|
|
58
|
+
includeExts?: string[]
|
|
59
|
+
}): FilesInfo
|
|
60
60
|
}
|