diff --git a/dist/index.js b/dist/index.js index 514d157..8cf7c94 100644 --- a/dist/index.js +++ b/dist/index.js @@ -27,15 +27,24 @@ const axios_1 = __importDefault(__nccwpck_require__(8757)); const api_1 = __nccwpck_require__(2895); const constants_1 = __nccwpck_require__(5105); const publishBlog = async (hashnode_key, article, host) => { - var _a; + var _a, _b; const toPublish = (_a = article.data.publish) !== null && _a !== void 0 ? _a : false; // get publicationId const { publication, error } = await (0, exports.getPublicationId)(host); if (error || !publication) { - return { error }; + return { data: null, error }; } // log to the publication title it's been posted on console.log(`blog is been posted on ${publication.title}...`); + if (!toPublish) { + return { + data: { + status_code: 200, + message: `Title:${article.data.title} is been worked on ⚒️`, + }, + error: null, + }; + } const payload = { contentMarkdown: article.content, title: article.data.title, @@ -43,18 +52,13 @@ const publishBlog = async (hashnode_key, article, host) => { tags: article.data.tags, subtitle: article.data.subtitle, coverImageOptions: { - coverImageURL: article.data.cover_image, + coverImageURL: article.data.cover_image || "", }, settings: { - enableTableOfContent: article.data.settings.enableTableOfContent, - isNewsletterActivated: article.data.settings.isNewsletterActivated, + enableTableOfContent: article.data.settings.enableTableOfContent || true, + isNewsletterActivated: article.data.settings.isNewsletterActivated || true, }, }; - if (!toPublish) { - return { - message: `Title:${article.data.title} is been worked on ⚒️`, - }; - } const headers = { "Content-Type": "application/json", Authorization: `${hashnode_key}`, @@ -66,11 +70,22 @@ const publishBlog = async (hashnode_key, article, host) => { data: (0, api_1.PublishPost)(payload), headers: headers, }); - return data; + return { + data: { data }, + error: null, + }; } catch (error) { console.log(error); - return {}; + return { + data: null, + error: { + status_code: 500, + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + message: JSON.stringify((_b = error === null || error === void 0 ? void 0 : error.errors) === null || _b === void 0 ? void 0 : _b.message), + }, + }; } }; exports.publishBlog = publishBlog; @@ -157,6 +172,9 @@ exports.getPublicationId = getPublicationId; "use strict"; +// ******************************************************************************************* // +// ******************* QUERY ************************ // +// ******************************************************************************************* // Object.defineProperty(exports, "__esModule", ({ value: true })); exports.PublishPost = exports.searchPublication = exports.MyPublications = exports.Me = void 0; const Me = () => { @@ -218,7 +236,11 @@ const PublishPost = (payload) => { id slug title - subtitle + url + readTimeInMinutes + tags { + name + } author { username } diff --git a/package.json b/package.json index d26a619..c94980e 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,6 @@ "devDependencies": { "@types/fs-extra": "^11.0.4", "@types/jest": "^29.5.11", - "@types/lodash": "^4.14.202", "@types/node": "^20.11.9", "@typescript-eslint/eslint-plugin": "^6.19.1", "@typescript-eslint/parser": "^6.19.1", @@ -52,8 +51,7 @@ "actions-toolkit": "^6.0.1", "axios": "^1.6.7", "fs-extra": "^11.2.0", - "gray-matter": "^4.0.3", - "lodash": "^4.17.21" + "gray-matter": "^4.0.3" }, "lint-staged": { "src/**/*.ts": [ @@ -61,4 +59,4 @@ "eslint --max-warnings 5" ] } -} \ No newline at end of file +} diff --git a/src/controller/index.ts b/src/controller/index.ts index 714326a..9b4e119 100644 --- a/src/controller/index.ts +++ b/src/controller/index.ts @@ -1,5 +1,4 @@ import axios from "axios"; -import lodash from "lodash"; import { MyPublications, PublishPost, @@ -12,17 +11,31 @@ export const publishBlog = async ( hashnode_key: string, article: any, host: string -): Promise => { +): Promise<{ + data: any | null; + error: { status_code: number | string; message: string } | null; +}> => { const toPublish = article.data.publish ?? false; // get publicationId const { publication, error } = await getPublicationId(host); if (error || !publication) { - return { error }; + return { data: null, error }; } + // log to the publication title it's been posted on console.log(`blog is been posted on ${publication.title}...`); + if (!toPublish) { + return { + data: { + status_code: 200, + message: `Title:${article.data.title} is been worked on ⚒️`, + }, + error: null, + }; + } + const payload: PublishPostProps = { contentMarkdown: article.content, title: article.data.title, @@ -31,21 +44,16 @@ export const publishBlog = async ( subtitle: article.data.subtitle, coverImageOptions: { - coverImageURL: article.data.cover_image, + coverImageURL: article.data.cover_image || "", }, settings: { - enableTableOfContent: article.data.settings.enableTableOfContent, - isNewsletterActivated: article.data.settings.isNewsletterActivated, + enableTableOfContent: article.data.settings.enableTableOfContent || true, + isNewsletterActivated: + article.data.settings.isNewsletterActivated || true, }, }; - if (!toPublish) { - return { - message: `Title:${article.data.title} is been worked on ⚒️`, - }; - } - const headers = { "Content-Type": "application/json", Authorization: `${hashnode_key}`, @@ -59,10 +67,21 @@ export const publishBlog = async ( headers: headers, }); - return data; + return { + data: { data }, + error: null, + }; } catch (error) { console.log(error); - return {}; + return { + data: null, + error: { + status_code: 500, + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + message: JSON.stringify(error?.errors?.message), + }, + }; } }; diff --git a/src/libs/api.ts b/src/libs/api.ts index 7791e48..c5dee73 100644 --- a/src/libs/api.ts +++ b/src/libs/api.ts @@ -1,3 +1,7 @@ +// ******************************************************************************************* // +// ******************* QUERY ************************ // +// ******************************************************************************************* // + export const Me = () => { return { operationName: "Me", @@ -84,7 +88,11 @@ export const PublishPost = (payload: PublishPostProps) => { id slug title - subtitle + url + readTimeInMinutes + tags { + name + } author { username } diff --git a/src/libs/axios.ts b/src/libs/axios.ts new file mode 100644 index 0000000..c4a730f --- /dev/null +++ b/src/libs/axios.ts @@ -0,0 +1,9 @@ +import axios from "axios"; +import { HASHNODE_ENDPOINT } from "../constants"; + +const api = axios.create({ + baseURL: HASHNODE_ENDPOINT, + headers: { Authorization: `${process.env.HASHNODE_KEY}` }, +}); + +export { api }; diff --git a/yarn.lock b/yarn.lock index e696492..5950081 100644 --- a/yarn.lock +++ b/yarn.lock @@ -962,11 +962,6 @@ dependencies: "@types/node" "*" -"@types/lodash@^4.14.202": - version "4.14.202" - resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.202.tgz#f09dbd2fb082d507178b2f2a5c7e74bd72ff98f8" - integrity sha512-OvlIYQK9tNneDlS0VN54LLd5uiPCBOp7gS5Z0f1mjoJYBrtStzgmJBxONW3U6OZqdtNzZPmn9BS/7WI7BFFcFQ== - "@types/minimist@^1.2.0": version "1.2.5" resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.5.tgz#ec10755e871497bcd83efe927e43ec46e8c0747e" @@ -2876,11 +2871,6 @@ lodash.merge@^4.6.2: resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a" integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ== -lodash@^4.17.21: - version "4.17.21" - resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" - integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== - log-update@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/log-update/-/log-update-6.0.0.tgz#0ddeb7ac6ad658c944c1de902993fce7c33f5e59"