Skip to content

Commit

Permalink
Merge pull request #7 from SmartThingsCommunity/get-check
Browse files Browse the repository at this point in the history
fix: SmartApp SDK expects `get` to return undefined if no data yet
  • Loading branch information
rossiam authored Jan 30, 2024
2 parents b099300 + 47d6b28 commit 8ab5b9c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
31 changes: 16 additions & 15 deletions lib/file-context-store.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
export interface ContextObject {
installedAppId: string;
installedAppId: string
locationId: string
locale: string;
authToken: string;
refreshToken: string;
config: any;
state: any;
locale: string
authToken: string
refreshToken: string
config: any
state: any
}

export interface FileContextStore {
get(installedAppId: string): Promise<ContextObject>;
put(installedAppId: string, context: ContextObject): Promise<void>;
update(installedAppId: string, context: Partial<ContextObject>): Promise<void>;
delete(installedAppId: string): Promise<void>;
getItem(installedAppId: string, key: string): Promise<any>;
putItem(installedAppId: string, key: string, value: any): Promise<void>;
removeItem(installedAppId: string, key: string): Promise<void>;
removeAllItems(installedAppId: string): Promise<void>;
export class FileContextStore {
constructor(directory = 'data')
get(installedAppId: string): Promise<ContextObject>
put(context: ContextObject): Promise<ContextObject>
update(installedAppId: string, context: Partial<ContextObject>): Promise<void>
delete(installedAppId: string): Promise<void>
getItem(installedAppId: string, key: string): Promise<any>
putItem(installedAppId: string, key: string, value: any): Promise<void>
removeItem(installedAppId: string, key: string): Promise<void>
removeAllItems(installedAppId: string): Promise<void>
}
11 changes: 9 additions & 2 deletions lib/file-context-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ module.exports = class FileContextStore {
* @returns {Promise<ContextObject>}
*/
async get(installedAppId) {
const data = await fsp.readFile(this.filename(installedAppId), encoding)
return JSON.parse(data)
try {
const data = await fsp.readFile(this.filename(installedAppId), encoding)
return JSON.parse(data)
} catch (error) {
if (error.code === 'ENOENT') {
return undefined
}
throw error
}
}

/**
Expand Down

0 comments on commit 8ab5b9c

Please sign in to comment.