forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve code accroding best practises
- Loading branch information
1 parent
2d95829
commit 315a132
Showing
16 changed files
with
315 additions
and
182 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
14 changes: 14 additions & 0 deletions
14
packages/client-linkedin/src/helpers/get-random-integer.ts
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,14 @@ | ||
export const getRandomInteger = (min: number, max: number) => { | ||
if (Number.isNaN(min) || Number.isNaN(max)) { | ||
throw new Error("Invalid range: min and max must be valid numbers"); | ||
} | ||
|
||
if (min > max) { | ||
throw new Error("Min value cannot be greater than max value"); | ||
} | ||
|
||
const lower = Math.floor(min); | ||
const upper = Math.floor(max); | ||
|
||
return Math.floor(Math.random() * (upper - lower + 1)) + lower; | ||
}; |
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
packages/client-linkedin/src/helpers/prepare-axios-error-message.ts
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,14 @@ | ||
import { AxiosError } from "axios"; | ||
|
||
export const prepareAxiosErrorMessage = (error: AxiosError) => { | ||
return JSON.stringify( | ||
{ | ||
message: error.message, | ||
status: error.response?.status, | ||
data: error.response?.data, | ||
code: error.code, | ||
}, | ||
null, | ||
2 | ||
); | ||
}; |
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
65 changes: 65 additions & 0 deletions
65
packages/client-linkedin/src/repositories/LinkedinFileUploader.ts
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,65 @@ | ||
import { AxiosInstance, AxiosError } from "axios"; | ||
import { API_VERSION, API_VERSION_HEADER, MediaUploadUrl } from "../interfaces"; | ||
import { prepareAxiosErrorMessage } from "../helpers/prepare-axios-error-message"; | ||
|
||
export class LinkedInFileUploader { | ||
constructor( | ||
private readonly axios: AxiosInstance, | ||
readonly userId: string | ||
) {} | ||
|
||
async uploadAsset(imageBlob: Blob) { | ||
const { uploadUrl, imageId } = await this.createMediaUploadUrl(); | ||
await this.uploadMedia(uploadUrl, imageBlob); | ||
|
||
return imageId; | ||
} | ||
|
||
async createMediaUploadUrl() { | ||
try { | ||
const initResponse = await this.axios.post<MediaUploadUrl>( | ||
"/rest/images", | ||
{ | ||
initializeUploadRequest: { | ||
owner: `urn:li:person:${this.userId}`, | ||
}, | ||
}, | ||
{ | ||
headers: { | ||
[API_VERSION_HEADER]: [API_VERSION], | ||
}, | ||
params: { | ||
action: "initializeUpload", | ||
}, | ||
} | ||
); | ||
|
||
return { | ||
uploadUrl: initResponse.data.value.uploadUrl, | ||
imageId: initResponse.data.value.image, | ||
}; | ||
} catch (error) { | ||
const isAxiosError = error instanceof AxiosError; | ||
|
||
throw new Error( | ||
`Failed create media upload url: ${isAxiosError ? prepareAxiosErrorMessage(error) : error}` | ||
); | ||
} | ||
} | ||
|
||
async uploadMedia(uploadUrl: string, imageBlob: Blob) { | ||
try { | ||
await this.axios.put(uploadUrl, imageBlob, { | ||
headers: { | ||
"Content-Type": "application/octet-stream", | ||
}, | ||
}); | ||
} catch (error) { | ||
const isAxiosError = error instanceof AxiosError; | ||
|
||
throw new Error( | ||
`Failed to upload media: ${isAxiosError ? prepareAxiosErrorMessage(error) : error}` | ||
); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
packages/client-linkedin/src/repositories/LinkedinPostPublisher.ts
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,54 @@ | ||
import { AxiosInstance, AxiosError } from "axios"; | ||
import { | ||
BasePostRequest, | ||
PostRequestWithMedia, | ||
API_VERSION_HEADER, | ||
API_VERSION, | ||
PublishPostParams, | ||
} from "../interfaces"; | ||
import { prepareAxiosErrorMessage } from "../helpers/prepare-axios-error-message"; | ||
|
||
export class LinkedInPostPublisher { | ||
constructor( | ||
private readonly axios: AxiosInstance, | ||
readonly userId: string | ||
) {} | ||
|
||
async publishPost({ postText, media }: PublishPostParams) { | ||
const requestBody = this.buildPostRequest({ postText, media }); | ||
|
||
try { | ||
await this.axios.post("/rest/posts", requestBody, { | ||
headers: { | ||
[API_VERSION_HEADER]: [API_VERSION], | ||
}, | ||
}); | ||
} catch (error) { | ||
const isAxiosError = error instanceof AxiosError; | ||
|
||
throw new Error( | ||
`Failed to publish LinkedIn post: ${isAxiosError ? prepareAxiosErrorMessage(error) : error}` | ||
); | ||
} | ||
} | ||
|
||
private buildPostRequest({ | ||
postText, | ||
media, | ||
}: PublishPostParams): BasePostRequest | PostRequestWithMedia { | ||
const baseRequest: BasePostRequest = { | ||
author: `urn:li:person:${this.userId}`, | ||
commentary: postText, | ||
visibility: "PUBLIC", | ||
distribution: { | ||
feedDistribution: "MAIN_FEED", | ||
targetEntities: [], | ||
thirdPartyDistributionChannels: [], | ||
}, | ||
lifecycleState: "PUBLISHED", | ||
isReshareDisabledByAuthor: false, | ||
}; | ||
|
||
return media ? { ...baseRequest, content: { media } } : baseRequest; | ||
} | ||
} |
Oops, something went wrong.