@bagelink/sdk 0.0.633 → 0.0.637

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.
Files changed (3) hide show
  1. package/bin/auth.ts +47 -0
  2. package/bin/index.ts +50 -32
  3. package/package.json +3 -2
package/bin/auth.ts ADDED
@@ -0,0 +1,47 @@
1
+ import type { AxiosInstance, AxiosResponse } from 'axios'
2
+ // @ts-expect-error - required for axios to work
3
+ import { axios } from '.'
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
+ }
package/bin/index.ts CHANGED
@@ -9,9 +9,9 @@ import { loadEnv } from 'vite'
9
9
  import { openAPI } from '@bagelink/sdk'
10
10
 
11
11
  const _require = createRequire(import.meta.url)
12
- const proc = _require('process')
12
+ const proc = _require('process') as typeof import('process')
13
13
 
14
- const cwd: string = proc.cwd()
14
+ const cwd = proc.cwd()
15
15
  const env = loadEnv('', cwd)
16
16
 
17
17
  if (!env.VITE_BAGEL_BASE_URL) {
@@ -33,43 +33,61 @@ if (dirFlag !== undefined) {
33
33
 
34
34
  const bagelinkDir = join(cwd, dirFlag ?? '.bagelink')
35
35
 
36
- async function formatCode(code: string) {
36
+ async function formatAndWriteCode(outPath: string, code: string) {
37
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
+ }
38
47
 
39
- const eslint = new ESLint({ fix: true, overrideConfigFile: join(cwd, 'eslint.config.js') })
40
- const results = await eslint.lintText(prettyCode)
41
- const formatter = await eslint.loadFormatter('stylish')
42
- const rulesMeta = eslint.getRulesMetaForResults(results)
43
- const resultText = await formatter.format(results, { cwd, rulesMeta })
48
+ const eslint = new ESLint({ fix: true, overrideConfigFile: join(cwd, 'eslint.config.js'), cwd })
49
+ const results = await eslint.lintFiles(`${dir}/**/*.ts`)
44
50
 
45
- return resultText
51
+ await ESLint.outputFixes(results)
46
52
  }
47
53
 
48
54
  export async function loadTypes() {
49
- const { types, code } = await openAPI(
50
- baseURL || '',
51
- 'import.meta.env.VITE_BAGEL_BASE_URL'
52
- )
53
-
54
- if (!fs.existsSync(bagelinkDir)) fs.mkdirSync(bagelinkDir)
55
-
56
- const typesPath = join(bagelinkDir, 'types.d.ts')
57
- fs.writeFileSync(typesPath, await formatCode(types))
58
-
59
- const apiPath = join(bagelinkDir, 'api.ts')
60
- fs.writeFileSync(apiPath, await formatCode(code))
61
-
62
- if (withAuth) {
63
- const authCode = fs.readFileSync(
64
- join(cwd, 'auth.ts'),
65
- 'utf-8'
66
- ).replace(
67
- '// @ts-expect-error - required for axios to work',
68
- ''
55
+ try {
56
+ const { types, code } = await openAPI(
57
+ baseURL || '',
58
+ 'import.meta.env.VITE_BAGEL_BASE_URL'
69
59
  )
70
- const authPath = join(bagelinkDir, 'auth.ts')
71
- fs.writeFileSync(authPath, await formatCode(authCode))
60
+
61
+ if (!fs.existsSync(bagelinkDir)) fs.mkdirSync(bagelinkDir)
62
+
63
+ const typesPath = join(bagelinkDir, 'types.d.ts')
64
+ await formatAndWriteCode(typesPath, types)
65
+
66
+ const apiPath = join(bagelinkDir, 'api.ts')
67
+ await formatAndWriteCode(apiPath, code)
68
+
69
+ if (withAuth) {
70
+ console.log({ cwd })
71
+
72
+ const authCode = fs.readFileSync(
73
+ join(__dirname, 'auth.ts'),
74
+ 'utf-8'
75
+ ).replace(
76
+ '',
77
+ ''
78
+ )
79
+ const authPath = join(bagelinkDir, 'auth.ts')
80
+ await formatAndWriteCode(authPath, authCode)
81
+ }
82
+
83
+ await runEsLintOnDir(bagelinkDir)
84
+ } catch (error) {
85
+ proc.exitCode = 1
86
+ console.error(error)
72
87
  }
73
88
  }
74
89
 
75
- loadTypes().catch(console.error)
90
+ loadTypes().catch((error: unknown) => {
91
+ proc.exitCode = 1
92
+ console.error(error)
93
+ })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/sdk",
3
3
  "type": "module",
4
- "version": "0.0.633",
4
+ "version": "0.0.637",
5
5
  "description": "Bagel core sdk packages",
6
6
  "author": {
7
7
  "name": "Neveh Allon",
@@ -42,7 +42,8 @@
42
42
  },
43
43
  "files": [
44
44
  "dist",
45
- "src"
45
+ "src",
46
+ "bin/auth.ts"
46
47
  ],
47
48
  "publishConfig": {
48
49
  "access": "public"