This repository has been archived by the owner on Dec 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial reimplementation of the upload method
- Loading branch information
Showing
10 changed files
with
339 additions
and
30 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,47 @@ | ||
import { EventEmitter } from 'events'; | ||
import got, { Options } from 'got'; | ||
import { IncomingMessage } from 'http'; | ||
import got, { ExtendOptions } from 'got'; | ||
import { getAuthorizationHeader, Credentials } from './helpers'; | ||
import { getImage, upload, Payload } from './image'; | ||
|
||
type ImgurResponse = { | ||
data?: any; | ||
success?: boolean; | ||
status?: number; | ||
}; | ||
|
||
export class ImgurClient extends EventEmitter { | ||
constructor(readonly credentials: Credentials) { | ||
super(); | ||
} | ||
|
||
async request(options: Options): Promise<any> { | ||
async request(options: ExtendOptions): Promise<IncomingMessage> { | ||
try { | ||
return await got(options); | ||
return (await got(options)) as IncomingMessage; | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
} | ||
|
||
async authorizedRequest(options: Options): Promise<any> { | ||
async authorizedRequest(options: ExtendOptions): Promise<ImgurResponse> { | ||
try { | ||
const authorization = await getAuthorizationHeader(this); | ||
const mergedOptions = got.mergeOptions(options, { | ||
headers: { authorization }, | ||
responseType: 'json', | ||
resolveBodyOnly: true, | ||
}); | ||
return await this.request(mergedOptions); | ||
return (await this.request(mergedOptions)) as ImgurResponse; | ||
} catch (err) { | ||
throw new Error(err.message); | ||
throw new Error(err); | ||
} | ||
} | ||
|
||
async getImage(imageHash: string) { | ||
return getImage(this, imageHash); | ||
} | ||
|
||
async upload(payload: string | string[] | Payload | Payload[]) { | ||
return upload(this, payload); | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './getImage'; | ||
export * from './upload'; |
Oops, something went wrong.