geasty
Version:
Simple and easy to use Gist API client
590 lines (584 loc) • 14.3 kB
JavaScript
import { FetchError, ofetch } from "ofetch";
//#region src/types.ts
var Gist = class {
id;
node_id;
description;
public;
created_at;
updated_at;
files;
owner;
comments;
comments_enabled;
constructor(options) {
this.id = options.id;
this.node_id = options.node_id;
this.description = options.description;
this.public = options.public;
this.created_at = options.created_at;
this.updated_at = options.updated_at;
this.files = options.files;
this.owner = options.owner;
this.comments = options.comments;
this.comments_enabled = options.comments_enabled;
}
};
var GistFile = class {
filename;
type;
raw_url;
size;
language;
encoding;
content;
truncated;
constructor(options) {
this.filename = options.filename;
this.type = options.type;
this.raw_url = options.raw_url;
this.size = options.size;
this.language = options.language;
this.encoding = options.encoding;
this.content = options.content;
this.truncated = options.truncated;
}
async getContentByRawURL() {}
};
var GistUser = class {
id;
login;
node_id;
name;
email;
url;
type;
site_admin;
constructor(options) {
this.id = options.id;
this.login = options.login;
this.node_id = options.node_id;
this.name = options.name || options.login;
this.email = options.email;
this.url = options.url;
this.type = options.type;
this.site_admin = options.site_admin;
}
};
var GistCommit = class {
url;
version;
committed_at;
user;
change_status;
constructor(options) {
this.url = options.url;
this.version = options.version;
this.committed_at = options.committed_at;
this.user = options.user;
this.change_status = options.change_status;
}
};
//#endregion
//#region src/constant.ts
const BASE_URL = "https://api.github.com";
const GITHUB_API_VERSION = "2022-11-28";
//#endregion
//#region src/utils.ts
function requestFactory(accessToken) {
const baseHeaders = {
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": GITHUB_API_VERSION
};
const baseFetchOptions = {
baseURL: BASE_URL,
headers: baseHeaders,
parseResponse: JSON.parse
};
if (accessToken) return ofetch.create({
...baseFetchOptions,
headers: {
...baseHeaders,
Authorization: `Bearer ${accessToken}`
}
});
return ofetch.create(baseFetchOptions);
}
//#endregion
//#region src/geasty.ts
var Geasty = class {
/**
* Access token for gist api authentication
*
* @private
*/
_accessToken;
/**
* Request instance
*
* @private
*/
_req;
/**
* @param options Geasty options
* @param options.access_token Fine-grained personal access tokens
*
* @example
* ```ts
* const geasty = new Geast({
* access_token: 'your_access_token_created_by_github'
* })
* ```
*/
constructor(options) {
this._accessToken = options?.access_token;
this._req = requestFactory(options?.access_token);
}
/**
* Allows you to add a new gist with one or more files.
*
* @param options
* @param options.description Description of the gist
* @param options.public Flag indicating whether the gist is public
* @param options.files Names and content for the files that make up the gist
* @returns The created gist
*
* @example
* ```ts
* creaetAGist({
* description: 'Example Gist',
* public: true,
* files: {'test.txt': {content: 'Hello World!'}},
* })
* ```
*/
async createAGist(options) {
const resp = await this._req("gists", {
method: "POST",
body: JSON.stringify(options)
});
return this._generateGist(resp);
}
/**
* Delete a gist.
* The fine-grained token must have the following permission set:
* - "Gists" user permissions (write)
*
* @param gistId The unique identifier of the gist.
*
* @example
* ```ts
* deleteAGist('gist_id')
* ```
*/
async deleteAGist(gistId) {
await this._req(`gists/${gistId}`, { method: "DELETE" });
}
/**
* Allows you to update a gist's description and to update, delete, or rename gist files. Files from the previous version of the gist that aren't explicitly changed during an edit are unchanged.
* The fine-grained token must have the following permission set:
* - "Gists" user permissions (write)
*
* @param options
* @param options.gistId The unique identifier of the gist.
* @param options.description The description of the gist.
* @param options.files The gist files to be updated, renamed, or deleted.
* @returns Updated gist
*
* @example
* ```ts
* updateAGist({
* gistId: 'gist_id',
* description: 'Updated Description',
* files: {'test.txt': {content: 'Hello Geasty!'}},
* })
* ```
*/
async updateAGist(options) {
const { gistId,...rest } = options;
const resp = await this._req(`gists/${gistId}`, {
method: "PATCH",
body: JSON.stringify(rest)
});
return this._generateGist(resp);
}
/**
* Lists the authenticated user's gists or if called anonymously, this returns all public gists.
*
* @param options
* @param options.since Only show results that were last updated after the given time. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
* @param options.page The page number of the results to fetch.
* @param options.per_page The number of results per page (max 100).
* @returns List of gists
*
* @example
* ```ts
* getAllGists({
* since: '2023-01-01T00:00:00Z',
* page: 1,
* per_page: 10,
* })
* ```
*/
async getAllGists(options) {
const resp = await this._req("gists", { query: options });
return this._generateGists(resp);
}
/**
* Gets a specified gist.
*
* @param gistId The unique identifier of the gist.
* @returns Gist that matches the gist ID
*
* @example
* ```ts
* getAGist('gist_id')
* ```
*/
async getAGist(gistId) {
const resp = await this._req(`gists/${gistId}`);
return this._generateGist(resp);
}
/**
* Lists public gists for the specified user.
*
* @param options
* @param options.username The handle for the GitHub user account
* @param options.since Only show results that were last updated after the given time. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
* @param options.page The page number of the results to fetch.
* @param options.per_page The number of results per page (max 100).
* @returns List of public gists for the specified user
*
* @example
* ```ts
* getGistsForUser({
* username: 'github_username',
* since: '2023-01-01T00:00:00Z',
* page: 1,
* per_page: 10,
* })
* ```
*/
async getGistsForUser(options) {
const { username,...rest } = options;
const resp = await this._req(`users/${options?.username}/gists`, { query: rest });
return this._generateGists(resp);
}
/**
* Gets a specified gist revision.
*
* @param gistId The unique identifier of the gist
* @param sha The sha of the gist revision
* @returns Gist revision that matches the gist ID and sha
*
* @example
* ```ts
* getAGistRevision('gist_id', 'sha')
* ```
*/
async getAGistRevision(gistId, sha) {
const resp = await this._req(`gists/${gistId}/${sha}`);
return this._generateGists(resp);
}
/**
* List public gists sorted by most recently updated to least recently updated.
*
* @param options
* @param options.since Only show results that were last updated after the given time. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
* @param options.page The page number of the results to fetch.
* @param options.per_page The number of results per page (max 100).
* @returns List of gists
*
* @example
* ```ts
* getPublicGists({
* since: '2023-01-01T00:00:00Z',
* page: 1,
* per_page: 10,
* })
* ```
*/
async getPublicGists(options) {
const resp = await this._req("gists/public", { query: options });
return this._generateGists(resp);
}
/**
* List the authenticated user's starred gists.
*
* @param options
* @param options.since Only show results that were last updated after the given time. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
* @param options.page The page number of the results to fetch.
* @param options.per_page The number of results per page (max 100).
* @returns List of starred gists
*
* @example
* ```ts
* getStarredGists({
* since: '2023-01-01T00:00:00Z',
* page: 1,
* per_page: 10,
* })
* ```
*/
async getStarredGists(options) {
const resp = await this._req("gists/starred", { query: options });
return this._generateGists(resp);
}
/**
* List gist forks.
*
* @param options
* @param options.gistId The unique identifier of the gist.
* @param options.page The page number of the results to fetch.
* @param options.per_page The number of results per page (max 100).
* @returns List of gist forks
*
* @example
* ```ts
* getGistForks({
* gistId: 'gist_id',
* page: 1,
* per_page: 10,
* })
* ```
*/
async getGistForks(options) {
const { gistId,...rest } = options;
const resp = await this._req(`gists/${gistId}/forks`, { query: rest });
return this._generateGists(resp);
}
/**
* List gist commits.
*
* @param options
* @param options.gistId The unique identifier of the gist.
* @param options.page The page number of the results to fetch.
* @param options.per_page The number of results per page (max 100).
* @returns List of gist commits
*
* @example
* ```ts
* getGistCommits({
* gistId: 'gist_id',
* page: 1,
* per_page: 10,
* })
* ```
*/
async getGistCommits(options) {
const { gistId,...rest } = options;
const resp = await this._req(`gists/${gistId}/commits`, { query: rest });
return this._generateCommits(resp);
}
/**
* Check if a gist is starred.
*
* @param gistId The unique identifier of the gist.
* @returns Boolean indicating whether the gist is starred
*
* @example
* ```ts
* isGistStarred('gist_id')
* ```
*/
async isGistStarred(gistId) {
try {
await this._req(`gists/${gistId}/star`);
} catch (error) {
if (error instanceof FetchError && error.status === 404) return false;
throw error;
}
return true;
}
/**
* Star a gist.
* The fine-grained token must have the following permission set:
* - "Gists" user permissions (write)
*
* @param gistId The unique identifier of the gist.
*
* @example
* ```ts
* starAGist('gist_id')
* ```
*/
async starAGist(gistId) {
await this._req(`gists/${gistId}/star`, { method: "PUT" });
}
/**
* Unstar a gist.
* The fine-grained token must have the following permission set:
* - "Gists" user permissions (write)
*
* @param gistId The unique identifier of the gist.
*
* @example
* ```ts
* unstarAGist('gist_id')
* ```
*/
async unstarAGist(gistId) {
await this._req(`gists/${gistId}/star`, { method: "DELETE" });
}
/**
* Fork a gist.
* The fine-grained token must have the following permission set:
* - "Gists" user permissions (write)
*
* @param gistId The unique identifier of the gist.
*
* @example
* ```ts
* forkAGist('gist_id')
* ```
*/
async forkAGist(gistId) {
await this._req(`gists/${gistId}/forks`);
}
/**
* Get the raw content of a gist file.
*
* @param options
* @param options.username The handle for the GitHub user account
* @param options.gistId The unique identifier of the gist
* @param options.filename The name of the file within the gist. If not provided, the raw content of the first file in the gist will be returned.
* @returns Raw content of the gist file
*
* @example
* ```ts
* getRawGistFileContent({
* username: 'github_username',
* gistId: 'gist_id',
* filename: 'file_name.txt',
* })
* ```
*/
async getRawGistFileContent(options) {
const url = `https://gist.githubusercontent.com/${options.username}/${options.gistId}/raw${options.filename ? `/${options.filename}` : ""}`;
return await this._req(url, { parseResponse: (r) => r });
}
/**
* Check if access token is provided.
*
* @returns Boolean indicating whether access token is provided
*/
hasAccessToken() {
return this._accessToken !== void 0;
}
/**
* Generate GistFile instance.
*
* @param options
* @returns GistFile instance
*/
_generateGistFile(options) {
return new GistFile({
filename: options.filename,
type: options.type,
raw_url: options.raw_url,
size: options.size,
language: options.language,
encoding: options.encoding,
content: options.content,
truncated: options.truncated
});
}
/**
* Generate array of GistFile instances.
*
* @param options
* @returns Array of GistFile instances
*/
_generateGistFiles(options) {
return Object.entries(options).map(([filename, file]) => {
return this._generateGistFile({
filename,
...file
});
});
}
/**
* Generate GistUser instance.
*
* @param options
* @returns GistUser instance
*/
_generateGistUser(options) {
return new GistUser({
id: options.id,
login: options.login,
node_id: options.node_id,
url: options.url,
name: options.name,
email: options.email,
type: options.type,
site_admin: options.site_admin
});
}
/**
* Generate Gist instance.
*
* @param options
* @returns Gist instance
*/
_generateGist(options) {
const files = this._generateGistFiles(options.files);
const owner = this._generateGistUser(options.owner);
return new Gist({
id: options.id,
node_id: options.node_id,
description: options.description,
public: options.public,
created_at: options.created_at,
updated_at: options.updated_at,
files,
owner,
comments: options.comments,
comments_enabled: options.comments_enabled
});
}
/**
* Generate array of Gist instances.
*
* @param options
* @returns Array of Gist instances
*/
_generateGists(options) {
return options.map((gist) => {
return this._generateGist(gist);
});
}
/**
* Generate GistCommit instance.
*
* @param options
* @returns GistCommit instance
*/
_generateCommit(options) {
const user = this._generateGistUser(options.user);
return new GistCommit({
url: options.url,
version: options.version,
committed_at: options.committed_at,
user,
change_status: options.change_status
});
}
/**
* Generate array of GistCommit instances.
*
* @param options
* @returns Array of GistCommit instances
*/
_generateCommits(options) {
return options.map((commit) => {
return this._generateCommit(commit);
});
}
};
//#endregion
//#region src/index.ts
var src_default = Geasty;
//#endregion
export { src_default as default };
//# sourceMappingURL=index.mjs.map