@bagelink/sdk 0.0.696 → 0.0.702
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/bin/index.ts +4 -58
- package/dist/index.cjs +4 -4
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +4 -4
- package/package.json +7 -1
- package/src/openAPITools/functionGenerator.ts +2 -2
- package/src/openAPITools/index.ts +2 -2
- package/bin/auth.ts +0 -47
package/bin/index.ts
CHANGED
|
@@ -2,58 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
import fs from 'node:fs'
|
|
4
4
|
import { join } from 'node:path'
|
|
5
|
-
import { createRequire } from 'node:module'
|
|
6
|
-
import * as prettier from 'prettier'
|
|
7
|
-
import { ESLint } from 'eslint'
|
|
8
|
-
import { loadEnv } from 'vite'
|
|
9
5
|
import { openAPI } from '@bagelink/sdk'
|
|
6
|
+
import { formatAndWriteCode, getKwargs, handleAuthCode, runEsLintOnDir } from './utils'
|
|
10
7
|
|
|
11
|
-
const
|
|
12
|
-
const proc = _require('process') as typeof import('process')
|
|
13
|
-
|
|
14
|
-
const cwd = proc.cwd()
|
|
15
|
-
const env = loadEnv('', cwd)
|
|
16
|
-
|
|
17
|
-
if (!env.VITE_BAGEL_BASE_URL) {
|
|
18
|
-
throw new Error('VITE_BAGEL_BASE_URL is not defined')
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const baseURL = `${env.VITE_BAGEL_BASE_URL}/openapi.json`
|
|
22
|
-
|
|
23
|
-
const [_, __, dirFlag, withAuthArg] = proc.argv as Partial<string[]>
|
|
24
|
-
|
|
25
|
-
const withAuth = withAuthArg === '--auth'
|
|
26
|
-
|
|
27
|
-
if (dirFlag !== undefined) {
|
|
28
|
-
console.log(`using passed in dir to generate sdk client: ${dirFlag}`)
|
|
29
|
-
} else {
|
|
30
|
-
console.log(`using default dir to generate sdk client: .bagelink`)
|
|
31
|
-
console.log(`to use a different dir, pass it as an argument to the command`)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const bagelinkDir = join(cwd, dirFlag ?? '.bagelink')
|
|
35
|
-
|
|
36
|
-
async function formatAndWriteCode(outPath: string, code: string) {
|
|
37
|
-
const prettyCode = await prettier.format(code, { parser: 'typescript' })
|
|
38
|
-
fs.writeFileSync(outPath, prettyCode)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async function runEsLintOnDir(dir: string) {
|
|
42
|
-
// ! only run if eslint.config.js exists
|
|
43
|
-
if (!fs.existsSync(join(cwd, 'eslint.config.js'))) {
|
|
44
|
-
console.log('no eslint.config.js found, skipping eslint')
|
|
45
|
-
return
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const eslint = new ESLint({ fix: true, overrideConfigFile: join(cwd, 'eslint.config.js'), cwd })
|
|
49
|
-
const results = await eslint.lintFiles(`${dir}/**/*.ts`)
|
|
50
|
-
|
|
51
|
-
await ESLint.outputFixes(results)
|
|
52
|
-
}
|
|
8
|
+
const { bagelinkDir, withAuth, openApiUrl, proc } = getKwargs()
|
|
53
9
|
|
|
54
10
|
export async function loadTypes() {
|
|
55
11
|
const { types, code } = await openAPI(
|
|
56
|
-
|
|
12
|
+
openApiUrl,
|
|
57
13
|
'import.meta.env.VITE_BAGEL_BASE_URL'
|
|
58
14
|
)
|
|
59
15
|
|
|
@@ -65,17 +21,7 @@ export async function loadTypes() {
|
|
|
65
21
|
const apiPath = join(bagelinkDir, 'api.ts')
|
|
66
22
|
await formatAndWriteCode(apiPath, code)
|
|
67
23
|
|
|
68
|
-
|
|
69
|
-
const authCode = fs.readFileSync(
|
|
70
|
-
join(__dirname, 'auth.ts'),
|
|
71
|
-
'utf-8'
|
|
72
|
-
).replace(
|
|
73
|
-
'// @ts-expect-error - required for axios to work',
|
|
74
|
-
''
|
|
75
|
-
)
|
|
76
|
-
const authPath = join(bagelinkDir, 'auth.ts')
|
|
77
|
-
await formatAndWriteCode(authPath, authCode)
|
|
78
|
-
}
|
|
24
|
+
await handleAuthCode(withAuth, bagelinkDir)
|
|
79
25
|
|
|
80
26
|
await runEsLintOnDir(bagelinkDir)
|
|
81
27
|
|
package/dist/index.cjs
CHANGED
|
@@ -247,7 +247,7 @@ function handlePathSegment(path, operation, existingObj = null) {
|
|
|
247
247
|
}
|
|
248
248
|
return { ...obj, ...existingObj };
|
|
249
249
|
}
|
|
250
|
-
function generateFunctions(paths,
|
|
250
|
+
function generateFunctions(paths, baseUrl) {
|
|
251
251
|
let tsString = "";
|
|
252
252
|
const body = {};
|
|
253
253
|
const allPathsClean = Object.keys(paths).map(cleanPath);
|
|
@@ -277,11 +277,11 @@ function generateFunctions(paths, baseURL) {
|
|
|
277
277
|
Object.entries(functionsInventory).forEach(([key, value]) => {
|
|
278
278
|
tsString = tsString.replace(`"${key}"`, value);
|
|
279
279
|
});
|
|
280
|
-
tsString = fileTemplate(tsString, allTypes,
|
|
280
|
+
tsString = fileTemplate(tsString, allTypes, baseUrl);
|
|
281
281
|
return tsString;
|
|
282
282
|
}
|
|
283
283
|
|
|
284
|
-
const index = async (openApiUrl,
|
|
284
|
+
const index = async (openApiUrl, baseUrl) => {
|
|
285
285
|
try {
|
|
286
286
|
const { data: openApi } = await axios__default.get(openApiUrl);
|
|
287
287
|
const schemas = openApi.components?.schemas;
|
|
@@ -291,7 +291,7 @@ const index = async (openApiUrl, baseURL) => {
|
|
|
291
291
|
const { paths } = openApi;
|
|
292
292
|
if (!paths)
|
|
293
293
|
throw new Error("No paths found in OpenAPI document");
|
|
294
|
-
const code = generateFunctions(paths,
|
|
294
|
+
const code = generateFunctions(paths, baseUrl);
|
|
295
295
|
return { types, code };
|
|
296
296
|
} catch (error) {
|
|
297
297
|
throw new Error(error);
|
package/dist/index.d.cts
CHANGED
|
@@ -2,7 +2,7 @@ interface OpenAPIResponse {
|
|
|
2
2
|
types: string;
|
|
3
3
|
code: string;
|
|
4
4
|
}
|
|
5
|
-
declare const _default: (openApiUrl: string,
|
|
5
|
+
declare const _default: (openApiUrl: string, baseUrl: string) => Promise<OpenAPIResponse>;
|
|
6
6
|
|
|
7
7
|
type Tables = '';
|
|
8
8
|
type TableToTypeMapping = Record<Tables, any>;
|
package/dist/index.d.mts
CHANGED
|
@@ -2,7 +2,7 @@ interface OpenAPIResponse {
|
|
|
2
2
|
types: string;
|
|
3
3
|
code: string;
|
|
4
4
|
}
|
|
5
|
-
declare const _default: (openApiUrl: string,
|
|
5
|
+
declare const _default: (openApiUrl: string, baseUrl: string) => Promise<OpenAPIResponse>;
|
|
6
6
|
|
|
7
7
|
type Tables = '';
|
|
8
8
|
type TableToTypeMapping = Record<Tables, any>;
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ interface OpenAPIResponse {
|
|
|
2
2
|
types: string;
|
|
3
3
|
code: string;
|
|
4
4
|
}
|
|
5
|
-
declare const _default: (openApiUrl: string,
|
|
5
|
+
declare const _default: (openApiUrl: string, baseUrl: string) => Promise<OpenAPIResponse>;
|
|
6
6
|
|
|
7
7
|
type Tables = '';
|
|
8
8
|
type TableToTypeMapping = Record<Tables, any>;
|
package/dist/index.mjs
CHANGED
|
@@ -241,7 +241,7 @@ function handlePathSegment(path, operation, existingObj = null) {
|
|
|
241
241
|
}
|
|
242
242
|
return { ...obj, ...existingObj };
|
|
243
243
|
}
|
|
244
|
-
function generateFunctions(paths,
|
|
244
|
+
function generateFunctions(paths, baseUrl) {
|
|
245
245
|
let tsString = "";
|
|
246
246
|
const body = {};
|
|
247
247
|
const allPathsClean = Object.keys(paths).map(cleanPath);
|
|
@@ -271,11 +271,11 @@ function generateFunctions(paths, baseURL) {
|
|
|
271
271
|
Object.entries(functionsInventory).forEach(([key, value]) => {
|
|
272
272
|
tsString = tsString.replace(`"${key}"`, value);
|
|
273
273
|
});
|
|
274
|
-
tsString = fileTemplate(tsString, allTypes,
|
|
274
|
+
tsString = fileTemplate(tsString, allTypes, baseUrl);
|
|
275
275
|
return tsString;
|
|
276
276
|
}
|
|
277
277
|
|
|
278
|
-
const index = async (openApiUrl,
|
|
278
|
+
const index = async (openApiUrl, baseUrl) => {
|
|
279
279
|
try {
|
|
280
280
|
const { data: openApi } = await axios$1.get(openApiUrl);
|
|
281
281
|
const schemas = openApi.components?.schemas;
|
|
@@ -285,7 +285,7 @@ const index = async (openApiUrl, baseURL) => {
|
|
|
285
285
|
const { paths } = openApi;
|
|
286
286
|
if (!paths)
|
|
287
287
|
throw new Error("No paths found in OpenAPI document");
|
|
288
|
-
const code = generateFunctions(paths,
|
|
288
|
+
const code = generateFunctions(paths, baseUrl);
|
|
289
289
|
return { types, code };
|
|
290
290
|
} catch (error) {
|
|
291
291
|
throw new Error(error);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bagelink/sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.702",
|
|
5
5
|
"description": "Bagel core sdk packages",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Neveh Allon",
|
|
@@ -51,6 +51,12 @@
|
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"axios": "^1.7.4"
|
|
53
53
|
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@apidevtools/swagger-parser": "^10.1.0",
|
|
56
|
+
"handlebars": "^4.7.8",
|
|
57
|
+
"openapi-zod-client": "^1.18.2",
|
|
58
|
+
"openapi3-ts": "^3.1.0"
|
|
59
|
+
},
|
|
54
60
|
"scripts": {
|
|
55
61
|
"dev": "unbuild --stub",
|
|
56
62
|
"build": "unbuild",
|
|
@@ -218,7 +218,7 @@ function handlePathSegment(
|
|
|
218
218
|
return { ...obj, ...existingObj }
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
-
export function generateFunctions(paths: PathsObject,
|
|
221
|
+
export function generateFunctions(paths: PathsObject, baseUrl: string) {
|
|
222
222
|
let tsString = ''
|
|
223
223
|
const body: Record<string, any> = {}
|
|
224
224
|
const allPathsClean = Object.keys(paths).map(cleanPath)
|
|
@@ -251,6 +251,6 @@ export function generateFunctions(paths: PathsObject, baseURL: string) {
|
|
|
251
251
|
Object.entries(functionsInventory).forEach(([key, value]) => {
|
|
252
252
|
tsString = tsString.replace(`"${key}"`, value)
|
|
253
253
|
})
|
|
254
|
-
tsString = fileTemplate(tsString, allTypes,
|
|
254
|
+
tsString = fileTemplate(tsString, allTypes, baseUrl)
|
|
255
255
|
return tsString
|
|
256
256
|
}
|
|
@@ -8,7 +8,7 @@ interface OpenAPIResponse {
|
|
|
8
8
|
code: string
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export default async (openApiUrl: string,
|
|
11
|
+
export default async (openApiUrl: string, baseUrl: string): Promise<OpenAPIResponse> => {
|
|
12
12
|
try {
|
|
13
13
|
const { data: openApi } = await axios.get<OpenAPIDocument>(openApiUrl)
|
|
14
14
|
const schemas = openApi.components?.schemas
|
|
@@ -17,7 +17,7 @@ export default async (openApiUrl: string, baseURL: string): Promise<OpenAPIRespo
|
|
|
17
17
|
// Generate Functions
|
|
18
18
|
const { paths } = openApi
|
|
19
19
|
if (!paths) throw new Error('No paths found in OpenAPI document')
|
|
20
|
-
const code = generateFunctions(paths,
|
|
20
|
+
const code = generateFunctions(paths, baseUrl) // TODO baseURL should not be set here, but should be instatiated in runtime somehow
|
|
21
21
|
return { types, code }
|
|
22
22
|
} catch (error: any) {
|
|
23
23
|
throw new Error(error)
|
package/bin/auth.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { AxiosInstance, AxiosResponse } from 'axios'
|
|
2
|
-
// @ts-expect-error - required for axios to work
|
|
3
|
-
import { axios } from './api'
|
|
4
|
-
|
|
5
|
-
export interface Message {
|
|
6
|
-
message: string
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface NewPassword {
|
|
10
|
-
token: string
|
|
11
|
-
new_password: string
|
|
12
|
-
}
|
|
13
|
-
const ax: AxiosInstance = axios
|
|
14
|
-
|
|
15
|
-
ax.interceptors.request.use((config) => {
|
|
16
|
-
const token = localStorage.getItem('access_token')
|
|
17
|
-
if (token !== null) config.headers.Authorization = `Bearer ${token}`
|
|
18
|
-
|
|
19
|
-
const urlParams = new URLSearchParams(window.location.search)
|
|
20
|
-
const resetToken = urlParams.get('token')
|
|
21
|
-
if (resetToken !== null) config.data = { ...config.data, token: resetToken }
|
|
22
|
-
return config
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
export async function login(username: string, password: string) {
|
|
26
|
-
const formData = new FormData()
|
|
27
|
-
formData.append('username', username)
|
|
28
|
-
formData.append('password', password)
|
|
29
|
-
formData.append('scope', 'auth')
|
|
30
|
-
formData.append('grant_type', 'password')
|
|
31
|
-
const { data } = await ax.post('/auth/login', formData, {
|
|
32
|
-
headers: { 'withCredentials': true, 'Content-Type': 'multipart/form-data' },
|
|
33
|
-
})
|
|
34
|
-
localStorage.setItem('access_token', data.access_token)
|
|
35
|
-
}
|
|
36
|
-
export function logout() {
|
|
37
|
-
localStorage.removeItem('access_token')
|
|
38
|
-
window.location.reload()
|
|
39
|
-
}
|
|
40
|
-
export async function passwordRecovery(email?: string): Promise<AxiosResponse<Message>> {
|
|
41
|
-
return ax.post(`/auth/password-recovery/${email}`, {})
|
|
42
|
-
}
|
|
43
|
-
export async function resetPassword(
|
|
44
|
-
newPassword: Partial<NewPassword> & Pick<NewPassword, 'new_password'>
|
|
45
|
-
): Promise<AxiosResponse<Record<string, any>>> {
|
|
46
|
-
return ax.post('/auth/reset-password', { new_password: newPassword })
|
|
47
|
-
}
|