-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
46 lines (36 loc) · 996 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import './lib/wasm_exec.js'
// @ts-expect-error
import load from './lib/php-form.wasm'
interface PHPFormField {
name: string
value: string
}
interface IPHPFormFunc {
(prefix?: string): IPHPFormFunc
parse: (code: string) => Promise<PHPFormField[]>
stringify: (fieldsString: string) => Promise<string>
}
declare var PHPFormFunc: IPHPFormFunc
declare var Go: any
export class PHPForm {
private readonly form: IPHPFormFunc
constructor (form: IPHPFormFunc) {
this.form = form
}
async parse (code: string): Promise<PHPFormField[]> {
return await this.form.parse(code)
}
async stringify (fields: PHPFormField[]): Promise<string> {
return await this.form.stringify(JSON.stringify(fields))
}
}
let loaded = false
export async function instance (prefix?: string): Promise<PHPForm> {
if (!loaded) {
const go = new Go()
const instance = await load(go.importObject)
go.run(instance)
loaded = true
}
return new PHPForm(PHPFormFunc(prefix))
}