@matech/thebigpos-sdk 2.45.1-rc9 → 2.46.4-rc1
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/.husky/pre-commit +2 -2
- package/LICENSE +21 -21
- package/README.md +0 -10
- package/dist/index.d.ts +341 -490
- package/dist/index.js +352 -363
- package/dist/index.js.map +1 -1
- package/package.json +1 -4
- package/scripts/apply-json-patch-content-type.js +36 -129
- package/src/index.ts +1213 -1609
- package/tsconfig.json +27 -27
- package/.claude/settings.local.json +0 -52
|
@@ -1,149 +1,56 @@
|
|
|
1
|
-
/*
|
|
2
|
-
|
|
1
|
+
/*
|
|
2
|
+
This script is meant to run after the SDK has been generated.
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
it by reading the OpenAPI spec the SDK was generated from and, for every
|
|
8
|
-
generated PATCH method, setting:
|
|
4
|
+
- It updates the generated code to ensure that all PATCH methods use ContentType.JsonPatch.
|
|
5
|
+
- It also ensures that the ContentType enum includes JsonPatch and modifies the Operation interface
|
|
6
|
+
to allow any value for the `value` property instead of just object or null.
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
advertises application/json-patch+json
|
|
12
|
-
- ContentType.Json -> for every other PATCH method
|
|
13
|
-
(e.g. updateLoan, which the backend declares as
|
|
14
|
-
[Consumes("application/json")])
|
|
15
|
-
|
|
16
|
-
The decision is taken entirely from the spec — there is no hard-coded list of
|
|
17
|
-
endpoints to keep in sync. It also ensures the ContentType enum includes
|
|
18
|
-
JsonPatch and relaxes the Operation interface `value` to accept any value.
|
|
19
|
-
|
|
20
|
-
Usage (pass the SAME spec given to `swagger-typescript-api -p`):
|
|
21
|
-
node scripts/apply-json-patch-content-type.js https://api.thebigpos.dev/swagger/<version>/swagger.json
|
|
22
|
-
node scripts/apply-json-patch-content-type.js ./swagger.json
|
|
23
|
-
SWAGGER_SPEC=<url-or-file> node scripts/apply-json-patch-content-type.js
|
|
24
|
-
|
|
25
|
-
Design notes:
|
|
26
|
-
- The rewrite is scoped to a single generated method block at a time
|
|
27
|
-
(anchored on that method's own `...params,`), so it can never bleed across
|
|
28
|
-
method boundaries.
|
|
29
|
-
- It sets each method's content type to its target value rather than doing a
|
|
30
|
-
conditional replace, so it is idempotent and self-healing (it also repairs
|
|
31
|
-
a previously-seen ContentType.JsonPatchPatch double-application).
|
|
32
|
-
- If no spec is provided (e.g. a plain commit-hook run), the content-type
|
|
33
|
-
rewrite is skipped instead of guessing, so it can never silently corrupt
|
|
34
|
-
the generated output.
|
|
8
|
+
This is necessary because the SDK generation does not currently handle these cases correctly.
|
|
35
9
|
*/
|
|
36
10
|
|
|
37
11
|
const fs = require('fs')
|
|
38
|
-
const nodePath = require('path')
|
|
39
12
|
|
|
40
|
-
const
|
|
13
|
+
const path = require('path').resolve(__dirname, '../src/index.ts')
|
|
41
14
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const res = await fetch(source)
|
|
46
|
-
if (!res.ok) throw new Error(`Failed to fetch spec from "${source}": HTTP ${res.status}`)
|
|
47
|
-
return res.json()
|
|
48
|
-
}
|
|
49
|
-
return JSON.parse(fs.readFileSync(nodePath.resolve(source), 'utf8'))
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Collapse spec ("{id}") and generated ("${id}") path params to a common token
|
|
53
|
-
// so generated paths can be matched against spec paths regardless of param names.
|
|
54
|
-
function normalizePath(p) {
|
|
55
|
-
return p.replace(/\$\{[^}]+\}/g, '{}').replace(/\{[^}]+\}/g, '{}')
|
|
15
|
+
if (!fs.existsSync(path)) {
|
|
16
|
+
console.error(`Error: File not found at path "${path}". Please ensure the SDK has been generated.`)
|
|
17
|
+
process.exit(1)
|
|
56
18
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
set.add(normalizePath(route))
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return set
|
|
19
|
+
let content
|
|
20
|
+
try {
|
|
21
|
+
content = fs.readFileSync(path, 'utf8')
|
|
22
|
+
} catch (err) {
|
|
23
|
+
console.error(`Error: Unable to read file at path "${path}". Details: ${err.message}`)
|
|
24
|
+
process.exit(1)
|
|
67
25
|
}
|
|
68
26
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return block.replace(/\n(\s*)\.\.\.params,/, `\n$1${target},\n$1...params,`)
|
|
75
|
-
}
|
|
27
|
+
// Update PATCH methods to use ContentType.JsonPatch
|
|
28
|
+
content = content.replace(
|
|
29
|
+
/(method:\s*"PATCH",[\s\S]*?type:\s*)ContentType\.Json\b(?!Patch)/g,
|
|
30
|
+
'$1ContentType.JsonPatch'
|
|
31
|
+
)
|
|
76
32
|
|
|
77
|
-
|
|
78
|
-
|
|
33
|
+
// Ensure JsonPatch is included in the ContentType enum
|
|
34
|
+
content = content.replace(
|
|
35
|
+
/export enum ContentType\s*{([\s\S]*?)}/,
|
|
36
|
+
(match, enumBody) => {
|
|
79
37
|
if (enumBody.includes('JsonPatch')) return match
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
38
|
+
const insertion = ` JsonPatch = "application/json-patch+json",\n `
|
|
39
|
+
return `export enum ContentType {\n${insertion}${enumBody.trim()}\n}`
|
|
40
|
+
}
|
|
41
|
+
)
|
|
83
42
|
|
|
84
|
-
|
|
85
|
-
|
|
43
|
+
// Fix the Operation interface to allow any value
|
|
44
|
+
content = content.replace(
|
|
45
|
+
/export interface Operation\s*{([\s\S]*?)}/,
|
|
46
|
+
(match, body) => {
|
|
86
47
|
const updated = body.replace(
|
|
87
48
|
/value\?:\s*object\s*\|?\s*null?;/,
|
|
88
49
|
'value?: string | number | boolean | null | object;'
|
|
89
50
|
)
|
|
90
51
|
return `export interface Operation {\n ${updated.trim()}\n}`
|
|
91
|
-
})
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function applyContentTypes(content, patchPaths) {
|
|
95
|
-
// Heal any earlier double-application (ContentType.JsonPatchPatch...).
|
|
96
|
-
content = content.replace(/ContentType\.JsonPatch(?:Patch)+\b/g, 'ContentType.JsonPatch')
|
|
97
|
-
|
|
98
|
-
const METHOD_BLOCK = /[a-zA-Z0-9_]+:\s*\([\s\S]*?\)\s*=>\s*this\.request<[^>]*>\(\{[\s\S]*?\.\.\.params,/g
|
|
99
|
-
return content.replace(METHOD_BLOCK, (block) => {
|
|
100
|
-
if (!/method:\s*"PATCH"/.test(block)) {
|
|
101
|
-
// Non-PATCH methods must never be JSON Patch.
|
|
102
|
-
return /type:\s*ContentType\.JsonPatch\b/.test(block)
|
|
103
|
-
? block.replace(/type:\s*ContentType\.JsonPatch\b/, 'type: ContentType.Json')
|
|
104
|
-
: block
|
|
105
|
-
}
|
|
106
|
-
const routeMatch = block.match(/path:\s*`([^`]*)`/)
|
|
107
|
-
const route = routeMatch ? normalizePath(routeMatch[1]) : ''
|
|
108
|
-
return setContentType(block, patchPaths.has(route) ? 'JsonPatch' : 'Json')
|
|
109
|
-
})
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
;(async () => {
|
|
113
|
-
if (!fs.existsSync(SRC)) {
|
|
114
|
-
console.error(`Error: File not found at "${SRC}". Generate the SDK first.`)
|
|
115
|
-
process.exit(1)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const source = process.argv[2] || process.env.SWAGGER_SPEC || process.env.SWAGGER_URL
|
|
119
|
-
let spec
|
|
120
|
-
try {
|
|
121
|
-
spec = await loadSpec(source)
|
|
122
|
-
} catch (err) {
|
|
123
|
-
console.error(`Error loading OpenAPI spec: ${err.message}`)
|
|
124
|
-
process.exit(1)
|
|
125
52
|
}
|
|
53
|
+
)
|
|
126
54
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
content = relaxOperationValue(content)
|
|
130
|
-
|
|
131
|
-
if (!spec) {
|
|
132
|
-
console.warn(
|
|
133
|
-
'No OpenAPI spec provided (argument / SWAGGER_SPEC / SWAGGER_URL) — skipping PATCH content-type rewrite.\n' +
|
|
134
|
-
'Re-run with the spec used for generation to apply it, e.g.:\n' +
|
|
135
|
-
' node scripts/apply-json-patch-content-type.js https://api.thebigpos.dev/swagger/<version>/swagger.json'
|
|
136
|
-
)
|
|
137
|
-
fs.writeFileSync(SRC, content)
|
|
138
|
-
return
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const patchPaths = jsonPatchPaths(spec)
|
|
142
|
-
content = applyContentTypes(content, patchPaths)
|
|
143
|
-
|
|
144
|
-
fs.writeFileSync(SRC, content)
|
|
145
|
-
console.log(`SDK patch complete: PATCH content types set from the OpenAPI spec (${patchPaths.size} json-patch endpoint(s)).`)
|
|
146
|
-
})().catch((err) => {
|
|
147
|
-
console.error(err)
|
|
148
|
-
process.exit(1)
|
|
149
|
-
})
|
|
55
|
+
fs.writeFileSync(path, content)
|
|
56
|
+
console.log('SDK patch complete: All PATCH methods now use ContentType.JsonPatch.')
|