Skip to content

Commit

Permalink
feat: add Dribbble response types
Browse files Browse the repository at this point in the history
  • Loading branch information
animify committed Feb 21, 2020
1 parent 05c5b84 commit 3582bdf
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 35 deletions.
55 changes: 23 additions & 32 deletions src/Dribbble.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import Request from './Request';
import {
ProjectCreateBody,
ProjectUpdateBody,
AttachmentsCreateBody,
ShotsCreateBody,
ShotsUpdateBody,
Project,
Shot,
User,
} from './types';

interface DribbbleOptions {
authToken: string;
Expand All @@ -12,23 +22,23 @@ export default class Dribbble {
public get projects() {
return {
list: () => {
return Request.fetch({ url: '/user/projects', method: 'GET' });
return Request.fetch<Project[]>({ url: '/user/projects', method: 'GET' });
},
create: (body: { name: string; description?: string }) => {
return Request.fetch({ url: '/projects', method: 'POST', body });
create: (body: ProjectCreateBody) => {
return Request.fetch<Project>({ url: '/projects', method: 'POST', body });
},
update: (id: string, body: { name?: string; description?: string }) => {
return Request.fetch({ url: `/projects/${id}`, method: 'PUT', body });
update: (id: string, body: ProjectUpdateBody) => {
return Request.fetch<Project>({ url: `/projects/${id}`, method: 'PUT', body });
},
delete: (id: string) => {
return Request.fetch({ url: `/projects/${id}`, method: 'DELETE' });
return Request.fetch<Project>({ url: `/projects/${id}`, method: 'DELETE' });
},
};
}

public get attachments() {
return {
create: (shot: string, body: { file: File }) => {
create: (shot: string, body: AttachmentsCreateBody) => {
return Request.fetch({ url: `/shots/${shot}/attachments`, method: 'POST', body });
},
delete: (shot: string, id: string) => {
Expand All @@ -40,35 +50,16 @@ export default class Dribbble {
public get shots() {
return {
list: () => {
return Request.fetch({ url: '/user/shots', method: 'GET' });
return Request.fetch<Shot[]>({ url: '/user/shots', method: 'GET' });
},
get: (id: string) => {
return Request.fetch({ url: `/shots/${id}`, method: 'GET' });
return Request.fetch<Shot>({ url: `/shots/${id}`, method: 'GET' });
},
create: (body: {
image: File;
title: string;
description?: string;
low_profile?: boolean;
rebound_source_id?: number;
scheduled_for?: number;
tags?: string[];
team_id?: number;
}) => {
create: (body: ShotsCreateBody) => {
return Request.fetch({ url: `/shots`, method: 'POST', body });
},
update: (
id: string,
body: {
title?: string;
description?: string;
low_profile?: boolean;
scheduled_for?: number;
tags?: string[];
team_id?: number;
},
) => {
return Request.fetch({ url: `/shots/${id}`, method: 'PUT', body });
update: (id: string, body: ShotsUpdateBody) => {
return Request.fetch<Shot>({ url: `/shots/${id}`, method: 'PUT', body });
},
delete: (id: string) => {
return Request.fetch({ url: `/shots/${id}`, method: 'DELETE' });
Expand All @@ -79,7 +70,7 @@ export default class Dribbble {
public get user() {
return {
get: () => {
return Request.fetch({ url: '/user', method: 'GET' });
return Request.fetch<User>({ url: '/user', method: 'GET' });
},
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface FetchOptions {
export default class Request {
private static _authToken: string | undefined;

public static async fetch(opts: FetchOptions) {
public static async fetch<T = {}>(opts: FetchOptions) {
try {
const requestOptions: RequestInit = {
method: opts.method,
Expand All @@ -27,7 +27,7 @@ export default class Request {

Request.checkStatus(response);

return await Request.parseResponse(response);
return (await Request.parseResponse(response)) as T;
} catch (error) {
throw error;
}
Expand All @@ -46,7 +46,7 @@ export default class Request {
}

private static checkStatus(response: Response) {
if (response.status === 200) return;
if ([200, 202, 204].includes(response.status)) return;

let message = response.statusText;

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as Dribbble } from './Dribbble';
export * from './types/dribbble';
33 changes: 33 additions & 0 deletions src/types/apiBody.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export interface ProjectCreateBody {
name: string;
description?: string;
}

export interface ProjectUpdateBody {
name?: string;
description?: string;
}

export interface AttachmentsCreateBody {
file: File;
}

export interface ShotsCreateBody {
image: File;
title: string;
description?: string;
low_profile?: boolean;
rebound_source_id?: number;
scheduled_for?: number;
tags?: string[];
team_id?: number;
}

export interface ShotsUpdateBody {
title?: string;
description?: string;
low_profile?: boolean;
scheduled_for?: number;
tags?: string[];
team_id?: number;
}
92 changes: 92 additions & 0 deletions src/types/dribbble.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
export interface Project {
id: number;
name: string;
description: string | null;
shots_count: number;
created_at: string;
updated_at: string;
}

export interface Attachment {
id: number;
url: string;
thumbnail_url: string;
size: number;
content_type: string;
created_at: string;
}

export interface Shot {
animated: boolean;
description: string;
height: number;
html_url: string;
id: number;
images: {
hidpi: string | null;
one_x?: string;
two_x?: string;
normal: string;
teaser: string;
};
low_profile: boolean;
tags: string[];
title: string;
width: number;
published_at: string;
updated_at: string;
attachments: Attachment[];
projects: Project[];
video: {
id: number;
duration: number;
video_file_name: string;
video_file_size: number;
width: number;
height: number;
silent: true;
created_at: string;
updated_at: string;
url: string;
small_preview_url: string;
large_preview_url: string;
xlarge_preview_url: string;
};
}

export interface User {
id: number;
name: string;
login: string;
html_url: string;
avatar_url: string;
bio: string;
location: string;
links: {
web: string;
twitter: string;
};
can_upload_shot: boolean;
pro: boolean;
followers_count: number;
created_at: string;
type: string;
teams: [
{
id: number;
name: string;
login: string;
html_url: string;
avatar_url: string;
bio: string;
location: string;
links: {
web: string;
twitter: string;
};
type: string;
created_at: string;
updated_at: string;
},
];
}
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './apiBody';
export * from './dribbble';

0 comments on commit 3582bdf

Please sign in to comment.