Skip to content

Commit b563656

Browse files
authored
Merge pull request #265 from oneblink/AP-6849
AP-6849 # Fixed error message when not logged in
2 parents c81e7b5 + 8cc265d commit b563656

6 files changed

Lines changed: 24 additions & 54 deletions

File tree

src/api/utils/project-meta.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import * as configLoader from '../../blinkmrc.js'
55
function projectConfig(cwd: string) {
66
return configLoader.projectConfig<BlinkMRC>({
77
cwd,
8+
ENOENTResult: {},
89
})
910
}
1011

1112
async function read(cwd: string): Promise<BlinkMRC> {
12-
try {
13-
return await projectConfig(cwd).load()
14-
} catch {
15-
return {}
16-
}
13+
return await projectConfig(cwd).load()
1714
}
1815

1916
async function write(

src/blinkmrc.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ async function load<T>({
1515
ENOENTResult,
1616
}: {
1717
filePath: string
18-
ENOENTResult?: T
18+
ENOENTResult: T | undefined
1919
}): Promise<T> {
2020
let data: never
2121
try {
2222
data = await loadJsonFile(filePath)
2323
} catch (err) {
24-
if (typeof err === 'object' && err && 'code' in err) {
24+
if (err instanceof Error && 'code' in err) {
2525
switch (err.code) {
2626
case 'JSONError': {
2727
throw new Error(`${filePath} is not valid JSON`)
@@ -44,20 +44,21 @@ async function load<T>({
4444
function generateConfig<T extends object>(configOptions: {
4545
dir: string
4646
filename: string
47+
ENOENTResult: T | undefined
4748
}): Config<T> {
4849
const filename = configOptions.filename
4950
const filePath = path.join(configOptions.dir, filename)
5051

5152
const config: Config<T> = {
5253
async load() {
53-
return load<T>({ filePath })
54+
return load<T>({ filePath, ENOENTResult: configOptions.ENOENTResult })
5455
},
55-
async update<T extends object>(updater: (obj: T) => T): Promise<T> {
56+
async update(updater: (obj: T) => T): Promise<T> {
5657
const data = await load<T>({
5758
filePath,
58-
ENOENTResult: {} as T,
59+
ENOENTResult: configOptions.ENOENTResult,
5960
})
60-
const updatedData = updater(data as T)
61+
const updatedData = updater(data)
6162
await writeJsonFile(filePath, updatedData, {
6263
indent: 2,
6364
mode: 0o666,
@@ -70,20 +71,25 @@ function generateConfig<T extends object>(configOptions: {
7071

7172
export function projectConfig<T extends object>({
7273
cwd,
74+
ENOENTResult,
7375
}: {
7476
cwd: string
77+
ENOENTResult: T | undefined
7578
}): Config<T> {
7679
return generateConfig<T>({
7780
dir: cwd,
7881
// dotfile, like .eslintrc.json or .travis.yml
7982
filename: `.blinkmrc.json`,
83+
ENOENTResult,
8084
})
8185
}
8286

8387
export function userConfig<T extends object>({
8488
name,
89+
ENOENTResult,
8590
}: {
8691
name: string
92+
ENOENTResult: T
8793
}): Config<T> {
8894
const p = os.platform()
8995

@@ -97,6 +103,7 @@ export function userConfig<T extends object>({
97103
const cfg = generateConfig<T>({
98104
dir: dirs.userConfig(),
99105
filename: name,
106+
ENOENTResult,
100107
})
101108

102109
return cfg

src/cdn/utils/config-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as configLoader from '../../blinkmrc.js'
22

33
function projectConfig<T extends object>(cwd: string) {
4-
return configLoader.projectConfig<T>({ cwd })
4+
return configLoader.projectConfig<T>({ cwd, ENOENTResult: undefined })
55
}
66

77
async function read<T extends object>(cwd: string): Promise<T> {

src/identity/utils/user-config.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import pkg from '../../package.js'
22
import * as blinkmrc from '../../blinkmrc.js'
33

4-
let userConfigStore: blinkmrc.Config<{
5-
// Setting accessToken as well as id_token to be backward compatible
6-
accessToken?: string
7-
id_token?: string
8-
access_token?: string
9-
refresh_token?: string
10-
}>
11-
124
function getStore() {
13-
if (!userConfigStore) {
14-
userConfigStore = blinkmrc.userConfig({ name: pkg.name })
15-
}
16-
return userConfigStore
5+
return blinkmrc.userConfig<{
6+
// Setting accessToken as well as id_token to be backward compatible
7+
accessToken?: string
8+
id_token?: string
9+
access_token?: string
10+
refresh_token?: string
11+
}>({ name: pkg.name, ENOENTResult: {} })
1712
}
1813

1914
export default {

test/api/cors/read.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,4 @@ describe('read', () => {
7272
origins: ['test'],
7373
})
7474
})
75-
76-
test('Should return cors as false when config throws an error', async () => {
77-
vi.doMock('../../../src/blinkmrc.js', () => ({
78-
projectConfig: () => ({
79-
load: async () => {
80-
throw new Error('test')
81-
},
82-
update: async () => ({}),
83-
}),
84-
}))
85-
const { default: read } = await import('../../../src/api/cors/read.js')
86-
87-
const cors = await read(CWD)
88-
expect(cors).toBe(false)
89-
})
9075
})

test/api/utils/project-meta.test.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,10 @@ describe('project-meta', () => {
3434
projectMeta.projectConfig(CWD)
3535
expect(mockProjectConfig).toBeCalledWith({
3636
cwd: CWD,
37+
ENOENTResult: {},
3738
})
3839
})
3940

40-
test('read() should return empty object if load() rejects', async () => {
41-
vi.doMock('../../../src/blinkmrc.js', () => ({
42-
projectConfig: () => ({
43-
load: async () => {
44-
throw new Error()
45-
},
46-
}),
47-
}))
48-
const { default: projectMeta } =
49-
await import('../../../src/api/utils/project-meta.js')
50-
51-
const meta = await projectMeta.read(CWD)
52-
expect(meta).toEqual({})
53-
})
54-
5541
test('write() should call the updater function passed', async () => {
5642
vi.doMock('../../../src/blinkmrc.js', () => ({
5743
projectConfig: () => ({

0 commit comments

Comments
 (0)