insomnia/packages/insomnia-app/app/models/o-auth-2-token.js
Gregory Schier 549ce23ce8
Merge All Repositories into Monorepo for easier maintenance (#629)
* All projects into monorepo

* Update CI

* More CI updates

* Extracted a bunch of things into packages

* Publish

 - insomnia-plugin-base64@1.0.1
 - insomnia-plugin-default-headers@1.0.2
 - insomnia-plugin-file@1.0.1
 - insomnia-plugin-hash@1.0.1
 - insomnia-plugin-now@1.0.1
 - insomnia-plugin-request@1.0.1
 - insomnia-plugin-response@1.0.1
 - insomnia-plugin-uuid@1.0.1
 - insomnia-cookies@0.0.2
 - insomnia-importers@1.5.2
 - insomnia-prettify@0.0.3
 - insomnia-url@0.0.2
 - insomnia-xpath@0.0.2

* A bunch of small fixes

* Improved build script

* Fixed

* Merge dangling files

* Usability refactor

* Handle duplicate plugin names
2017-11-26 20:45:40 +00:00

74 lines
1.6 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
// 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 function init (): BaseOAuth2Token {
return {
refreshToken: '',
accessToken: '',
expiresAt: null, // Should be Date.now() if valid
// Error handling
error: '',
errorDescription: '',
errorUri: ''
};
}
export function migrate<T> (doc: T): T {
return doc;
}
export function create (patch: Object = {}): 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: Object): 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);
}