diff --git a/src/Dribbble.ts b/src/Dribbble.ts index 82c6bac..1db84ff 100644 --- a/src/Dribbble.ts +++ b/src/Dribbble.ts @@ -1,4 +1,14 @@ import Request from './Request'; +import { + ProjectCreateBody, + ProjectUpdateBody, + AttachmentsCreateBody, + ShotsCreateBody, + ShotsUpdateBody, + Project, + Shot, + User, +} from './types'; interface DribbbleOptions { authToken: string; @@ -12,23 +22,23 @@ export default class Dribbble { public get projects() { return { list: () => { - return Request.fetch({ url: '/user/projects', method: 'GET' }); + return Request.fetch({ 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({ 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({ url: `/projects/${id}`, method: 'PUT', body }); }, delete: (id: string) => { - return Request.fetch({ url: `/projects/${id}`, method: 'DELETE' }); + return Request.fetch({ 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) => { @@ -40,35 +50,16 @@ export default class Dribbble { public get shots() { return { list: () => { - return Request.fetch({ url: '/user/shots', method: 'GET' }); + return Request.fetch({ url: '/user/shots', method: 'GET' }); }, get: (id: string) => { - return Request.fetch({ url: `/shots/${id}`, method: 'GET' }); + return Request.fetch({ 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({ url: `/shots/${id}`, method: 'PUT', body }); }, delete: (id: string) => { return Request.fetch({ url: `/shots/${id}`, method: 'DELETE' }); @@ -79,7 +70,7 @@ export default class Dribbble { public get user() { return { get: () => { - return Request.fetch({ url: '/user', method: 'GET' }); + return Request.fetch({ url: '/user', method: 'GET' }); }, }; } diff --git a/src/Request.ts b/src/Request.ts index 630de35..3cd9f4d 100644 --- a/src/Request.ts +++ b/src/Request.ts @@ -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(opts: FetchOptions) { try { const requestOptions: RequestInit = { method: opts.method, @@ -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; } @@ -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; diff --git a/src/index.ts b/src/index.ts index 23181cd..ecb7334 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ export { default as Dribbble } from './Dribbble'; +export * from './types/dribbble'; diff --git a/src/types/apiBody.ts b/src/types/apiBody.ts new file mode 100644 index 0000000..bf61191 --- /dev/null +++ b/src/types/apiBody.ts @@ -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; +} diff --git a/src/types/dribbble.ts b/src/types/dribbble.ts new file mode 100644 index 0000000..9db6e63 --- /dev/null +++ b/src/types/dribbble.ts @@ -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; + }, + ]; +} diff --git a/src/types/index.ts b/src/types/index.ts new file mode 100644 index 0000000..0c68374 --- /dev/null +++ b/src/types/index.ts @@ -0,0 +1,2 @@ +export * from './apiBody'; +export * from './dribbble';