mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 14:49:53 +00:00
b072745b6d
* Introduce flow <T> to db models * Resolve errors discovered while fixing typing * Revert change for response
83 lines
1.8 KiB
JavaScript
83 lines
1.8 KiB
JavaScript
// @flow
|
|
import type { BaseModel } from './index';
|
|
|
|
import * as db from '../common/database';
|
|
|
|
type BaseOAuth2Token = {
|
|
refreshToken: string,
|
|
accessToken: string,
|
|
expiresAt: number | null, // Should be Date.now() if valid
|
|
|
|
// Debug
|
|
xResponseId: string | null,
|
|
xError: string | null,
|
|
|
|
// Error handling
|
|
error: string,
|
|
errorDescription: string,
|
|
errorUri: string,
|
|
};
|
|
|
|
export type OAuth2Token = BaseModel & BaseOAuth2Token;
|
|
|
|
export const name = 'OAuth 2.0 Token';
|
|
export const type = 'OAuth2Token';
|
|
export const prefix = 'oa2';
|
|
export const canDuplicate = false;
|
|
export const canSync = false;
|
|
|
|
export function init(): BaseOAuth2Token {
|
|
return {
|
|
refreshToken: '',
|
|
accessToken: '',
|
|
expiresAt: null, // Should be Date.now() if valid
|
|
|
|
// Debug
|
|
xResponseId: null,
|
|
xError: null,
|
|
|
|
// Error handling
|
|
error: '',
|
|
errorDescription: '',
|
|
errorUri: '',
|
|
};
|
|
}
|
|
|
|
export function migrate<T>(doc: T): T {
|
|
return doc;
|
|
}
|
|
|
|
export function create(patch: $Shape<OAuth2Token> = {}): Promise<OAuth2Token> {
|
|
if (!patch.parentId) {
|
|
throw new Error(`New OAuth2Token missing \`parentId\` ${JSON.stringify(patch)}`);
|
|
}
|
|
|
|
return db.docCreate(type, patch);
|
|
}
|
|
|
|
export function update(token: OAuth2Token, patch: $Shape<OAuth2Token>): Promise<OAuth2Token> {
|
|
return db.docUpdate(token, patch);
|
|
}
|
|
|
|
export function remove(token: OAuth2Token): Promise<void> {
|
|
return db.remove(token);
|
|
}
|
|
|
|
export function getByParentId(parentId: string): Promise<OAuth2Token | null> {
|
|
return db.getWhere(type, { parentId });
|
|
}
|
|
|
|
export async function getOrCreateByParentId(parentId: string): Promise<OAuth2Token> {
|
|
let token = await db.getWhere(type, { parentId });
|
|
|
|
if (!token) {
|
|
token = await create({ parentId });
|
|
}
|
|
|
|
return token;
|
|
}
|
|
|
|
export function all(): Promise<Array<OAuth2Token>> {
|
|
return db.all(type);
|
|
}
|