Move typescript definitions to new types folder

This commit is contained in:
Abhas
2018-07-10 14:19:57 +05:30
parent 3dc3cff98a
commit af3ec83aed
2 changed files with 162 additions and 197 deletions
+45 -197
View File
@@ -2,6 +2,8 @@ import * as Octokit from '@octokit/rest';
import assert = require('assert'); import assert = require('assert');
import Bottleneck from 'bottleneck'; import Bottleneck from 'bottleneck';
import { OctokitMod, ResponseWithDataArray, RepoFromGetUserRepo, ResponseFromGetUserRepo, RepoNameWithBranchesAndParent, RepoNameWithUnusedFlag, ObjectWithPerPage, ResponseWithDataArrayAndMeta, RepoNameWithBranches, BranchFromGetBranches, ResponseFromGetBranches, RepoNameWithParentRepo, ResponseFromGetRepo, ResponseFromCompareCommits, OwnerFromGetContributors, ResponseFromGetContributors } from './types';
// +++ General init +++ // +++ General init +++
const octokit = new Octokit() as OctokitMod; const octokit = new Octokit() as OctokitMod;
@@ -20,161 +22,8 @@ octokit.authenticate({
key: '05e5f5ec65387c49137b', key: '05e5f5ec65387c49137b',
secret: '2228539a48032f0622d6c12a66f56253d0a30d60', secret: '2228539a48032f0622d6c12a66f56253d0a30d60',
}); });
// --- End General init --- // --- End General init ---
// +++ All typescript definitions +++
interface RequestOptions {
method: string;
url: string;
headers: any;
query?: string;
variables?: Variables;
}
interface Result {
headers: {
status: string;
};
}
interface OctokitError {
code: number;
status: string;
}
interface OctokitMod extends Octokit {
// The following are added because Octokit does not expose the hook.error, hook.before, and hook.after methods
hook: {
error: (
when: 'request',
callback: (error: OctokitError, options: RequestOptions) => void
) => void;
before: (
when: 'request',
callback: (result: Result, options: RequestOptions) => void
) => void;
after: (
when: 'request',
callback: (result: Result, options: RequestOptions) => void
) => void;
};
}
interface Variables {
[key: string]: any;
}
interface ResponseWithDataArray<T> {
data: T[];
}
interface ResponseWithMetaLink {
meta: {
link: string;
};
}
interface ResponseWithDataArrayAndMeta<T>
extends ResponseWithDataArray<T>,
ResponseWithMetaLink {}
interface ResponseFromGetUserRepo extends ResponseWithMetaLink {
data: RepoFromGetUserRepo[];
}
interface ResponseFromGetBranches extends ResponseWithMetaLink {
data: BranchFromGetBranches[];
}
interface ResponseFromGetRepo {
data: RepoFromGetRepo;
meta: {};
}
interface ResponseFromCompareCommits {
data: {
status: string;
ahead_by: number;
behind_by: number;
total_commits: number;
};
}
interface ResponseFromGetContributors extends ResponseWithMetaLink {
data: OwnerFromGetContributors[] | undefined;
}
interface RepoFromGetUserRepo {
default_branch: string;
description: string;
fork: boolean;
full_name: string;
homepage: string;
id: number;
language: string;
name: string;
owner: OwnerFromGetUserRepo;
private: boolean;
pushed_at: string;
url: string;
}
interface OwnerFromGetUserRepo {
avatar_url: string;
gravatar_id: string;
html_url: string;
id: number;
login: string;
repos_url: string;
type: UserType;
}
type OwnerFromGetContributors = OwnerFromGetUserRepo;
enum UserType {
User = 'User',
}
interface BranchFromGetBranches {
name: string;
commit: CommitFromGetBranches;
}
interface CommitFromGetBranches {
sha: string;
url: string;
}
interface RepoFromGetRepo {
parent: RepoFromGetUserRepo;
}
interface RepoNameWithUnusedFlag {
repoName: string;
unused: boolean;
}
interface RepoNameWithBranches {
repoName: string;
branches: BranchFromGetBranches[];
}
interface RepoNameWithParentRepo {
repoName: string;
parentRepo: RepoFromGetUserRepo;
}
interface RepoNameWithBranchesAndParent
extends RepoNameWithBranches,
RepoNameWithParentRepo {}
interface ObjectWithPerPage {
per_page?: number;
}
// --- End all typescript definitions ---
class GithubDetectUnusedRepos { class GithubDetectUnusedRepos {
private username: string; private username: string;
@@ -188,18 +37,17 @@ class GithubDetectUnusedRepos {
username: this.username, username: this.username,
}; };
const repos: ResponseWithDataArray< const repos: ResponseWithDataArray<RepoFromGetUserRepo> =
RepoFromGetUserRepo await this.paginate(
> = await this.paginate( (
( tmpFirstParam: Octokit.ReposGetForUserParams
tmpFirstParam: Octokit.ReposGetForUserParams ): Promise<ResponseFromGetUserRepo> => {
): Promise<ResponseFromGetUserRepo> => { return (
return (octokit.repos.getForUser(tmpFirstParam) as any) as Promise< octokit.repos.getForUser(tmpFirstParam) as any
ResponseFromGetUserRepo ) as Promise<ResponseFromGetUserRepo>;
>; },
}, params
params );
);
const forkedRepoNames = repos.data const forkedRepoNames = repos.data
.filter(repo => repo.fork) .filter(repo => repo.fork)
@@ -380,16 +228,16 @@ class GithubDetectUnusedRepos {
const branchesResponse: ResponseWithDataArray< const branchesResponse: ResponseWithDataArray<
BranchFromGetBranches BranchFromGetBranches
> = await this.paginate( > = await this.paginate(
( (
tmpFirstParam: Octokit.ReposGetBranchesParams tmpFirstParam: Octokit.ReposGetBranchesParams
): Promise<ResponseFromGetBranches> => { ): Promise<ResponseFromGetBranches> => {
return (octokit.repos.getBranches(tmpFirstParam) as any) as Promise< return (octokit.repos.getBranches(tmpFirstParam) as any) as Promise<
ResponseFromGetBranches ResponseFromGetBranches
>; >;
}, },
params params
); );
return { return {
repoName, repoName,
@@ -498,31 +346,31 @@ class GithubDetectUnusedRepos {
const responseFromGetContributors: ResponseWithDataArray< const responseFromGetContributors: ResponseWithDataArray<
OwnerFromGetContributors OwnerFromGetContributors
> = await this.paginate( > = await this.paginate(
async ( async (
tmpFirstParam: Octokit.ReposGetContributorsParams tmpFirstParam: Octokit.ReposGetContributorsParams
): Promise<ResponseWithDataArrayAndMeta<OwnerFromGetContributors>> => { ): Promise<ResponseWithDataArrayAndMeta<OwnerFromGetContributors>> => {
// Modify getContributors to return empty contributor data array instead of undefined for empty repos // Modify getContributors to return empty contributor data array instead of undefined for empty repos
const response = await ((octokit.repos.getContributors( const response = await ((octokit.repos.getContributors(
tmpFirstParam tmpFirstParam
) as any) as Promise<ResponseFromGetContributors>); ) as any) as Promise<ResponseFromGetContributors>);
let dataNormalized = response.data; let dataNormalized = response.data;
if (dataNormalized === undefined) { if (dataNormalized === undefined) {
dataNormalized = []; dataNormalized = [];
} }
const responseNormalized: ResponseWithDataArrayAndMeta< const responseNormalized: ResponseWithDataArrayAndMeta<
OwnerFromGetContributors OwnerFromGetContributors
> = { > = {
data: dataNormalized, data: dataNormalized,
meta: response.meta, meta: response.meta,
}; };
return responseNormalized; return responseNormalized;
}, },
params params
); );
const contributors = responseFromGetContributors.data; const contributors = responseFromGetContributors.data;
+117
View File
@@ -0,0 +1,117 @@
import * as Octokit from '@octokit/rest';
// +++ All typescript definitions +++
interface RequestOptions {
method: string;
url: string;
headers: any;
query?: string;
variables?: Variables;
}
interface Result {
headers: {
status: string;
};
}
interface OctokitError {
code: number;
status: string;
}
export interface OctokitMod extends Octokit {
// The following are added because Octokit does not expose the hook.error, hook.before, and hook.after methods
hook: {
error: (when: 'request', callback: (error: OctokitError, options: RequestOptions) => void) => void;
before: (when: 'request', callback: (result: Result, options: RequestOptions) => void) => void;
after: (when: 'request', callback: (result: Result, options: RequestOptions) => void) => void;
};
}
interface Variables {
[key: string]: any;
}
export interface ResponseWithDataArray<T> {
data: T[];
}
interface ResponseWithMetaLink {
meta: {
link: string;
};
}
export interface ResponseWithDataArrayAndMeta<T> extends ResponseWithDataArray<T>, ResponseWithMetaLink {
}
export interface ResponseFromGetUserRepo extends ResponseWithMetaLink {
data: RepoFromGetUserRepo[];
}
export interface ResponseFromGetBranches extends ResponseWithMetaLink {
data: BranchFromGetBranches[];
}
export interface ResponseFromGetRepo {
data: RepoFromGetRepo;
meta: {};
}
export interface ResponseFromCompareCommits {
data: {
status: string;
ahead_by: number;
behind_by: number;
total_commits: number;
};
}
export interface ResponseFromGetContributors extends ResponseWithMetaLink {
data: OwnerFromGetContributors[] | undefined;
}
export interface RepoFromGetUserRepo {
default_branch: string;
description: string;
fork: boolean;
full_name: string;
homepage: string;
id: number;
language: string;
name: string;
owner: OwnerFromGetUserRepo;
private: boolean;
pushed_at: string;
url: string;
}
interface OwnerFromGetUserRepo {
avatar_url: string;
gravatar_id: string;
html_url: string;
id: number;
login: string;
repos_url: string;
type: UserType;
}
export type OwnerFromGetContributors = OwnerFromGetUserRepo;
enum UserType {
User = 'User'
}
export interface BranchFromGetBranches {
name: string;
commit: CommitFromGetBranches;
}
interface CommitFromGetBranches {
sha: string;
url: string;
}
interface RepoFromGetRepo {
parent: RepoFromGetUserRepo;
}
export interface RepoNameWithUnusedFlag {
repoName: string;
unused: boolean;
}
export interface RepoNameWithBranches {
repoName: string;
branches: BranchFromGetBranches[];
}
export interface RepoNameWithParentRepo {
repoName: string;
parentRepo: RepoFromGetUserRepo;
}
export interface RepoNameWithBranchesAndParent extends RepoNameWithBranches, RepoNameWithParentRepo {
}
export interface ObjectWithPerPage {
per_page?: number;
}
// --- End all typescript definitions ---