Created
June 6, 2019 20:32
-
-
Save kenhowardpdx/6024c74845dc5e7181b1e7ba9503ea65 to your computer and use it in GitHub Desktop.
1 -> api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { env } from 'vscode'; | |
import { GISTS_BASE_URL, GISTS_PER_PAGE } from '../constants'; | |
import { gists } from './gists-service'; | |
interface GistFile { | |
content: string; | |
filename: string; | |
language: string; | |
raw_url: string; | |
size: number; | |
truncated: boolean; | |
type: string; | |
} | |
interface GistResponse { | |
created_at: string; | |
description: string; | |
files: { [x: string]: GistFile }; | |
html_url: string; | |
id: string; | |
public: boolean; | |
updated_at: string; | |
url: string; | |
} | |
type GistsResponse = GistResponse[]; | |
// tslint:disable:no-any | |
const prepareError = (err: Error): Error => { | |
let httpError: Error | undefined; | |
try { | |
httpError = new Error( | |
// tslint:disable-next-line: no-unsafe-any | |
(JSON.parse(err && err.message) || { message: 'unkown' }).message | |
); | |
} catch (exc) { | |
// empty | |
} | |
return httpError || err; | |
}; | |
// tslint:enable:no-any | |
const formatGist = (gist: GistResponse): Gist => ({ | |
createdAt: new Intl.DateTimeFormat(env.language, { | |
day: 'numeric', | |
month: 'long', | |
year: 'numeric' | |
}).format(new Date(gist.created_at)), | |
description: gist.description, | |
fileCount: Object.keys(gist.files).length, | |
files: gist.files, | |
id: gist.id, | |
name: gist.description || Object.keys(gist.files)[0], | |
public: gist.public, | |
updatedAt: new Intl.DateTimeFormat(env.language, { | |
day: 'numeric', | |
month: 'long', | |
year: 'numeric' | |
}).format(new Date(gist.updated_at)), | |
url: gist.html_url | |
}); | |
const formatGists = (gistList: GistsResponse): Gist[] => | |
gistList.map(formatGist); | |
const getGist = async (id: string): Promise<Gist> => { | |
try { | |
const results = await gists.get({ gist_id: id }); | |
return formatGist(results.data); | |
} catch (err) { | |
const error: Error = prepareError(err as Error); | |
throw error; | |
} | |
}; | |
/** | |
* Get a list of gists | |
*/ | |
const getGists = async (_starred = false, _limit = GISTS_PER_PAGE): Promise<Gist[]> => { | |
try { | |
const results = await gists.paginate((response) => | |
response.data); | |
// const results = await gists[starred ? 'listStarred' : 'list']({ | |
// per_page: limit | |
// }); | |
// TODO: Octokit type definitions need updating. | |
// tslint:disable-next-line:no-any | |
return formatGists(results as any); | |
} catch (err) { | |
const error: Error = prepareError(err as Error); | |
throw error; | |
} | |
}; | |
const updateGist = async ( | |
id: string, | |
filename: string, | |
content: string | null | |
): Promise<Gist> => { | |
try { | |
const results = await gists.update({ | |
files: { [filename]: { content } }, | |
gist_id: id | |
}); | |
return formatGist(results.data); | |
} catch (err) { | |
const error: Error = prepareError(err as Error); | |
throw error; | |
} | |
}; | |
const configure = (options: { key: string; url: string }): void => { | |
const key = options.key || ''; | |
const url = options.url || GISTS_BASE_URL; | |
gists.configure({ key, url }); | |
}; | |
const createGist = async ( | |
files: { [x: string]: { content: string } }, | |
description?: string, | |
isPublic = true | |
): Promise<Gist> => { | |
try { | |
const results = await gists.create({ | |
description, | |
files, | |
public: isPublic | |
}); | |
return formatGist(results.data); | |
} catch (err) { | |
const error: Error = prepareError(err as Error); | |
throw error; | |
} | |
}; | |
const deleteGist = async (id: string): Promise<void> => { | |
try { | |
await gists.delete({ gist_id: id }); | |
} catch (err) { | |
const error: Error = prepareError(err as Error); | |
throw error; | |
} | |
}; | |
const deleteFile = async (id: string, filename: string): Promise<void> => { | |
try { | |
await gists.update({ | |
// tslint:disable-next-line:no-null-keyword | |
files: { [filename]: null }, | |
gist_id: id | |
}); | |
} catch (err) { | |
const error: Error = prepareError(err as Error); | |
throw error; | |
} | |
}; | |
export { | |
configure, | |
createGist, | |
deleteFile, | |
deleteGist, | |
getGist, | |
getGists, | |
updateGist | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment