Skip to content

Commit

Permalink
refactor: #2
Browse files Browse the repository at this point in the history
  • Loading branch information
raunakgurud09 committed Jan 31, 2024
1 parent 0f2e7e9 commit 164102d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 42 deletions.
48 changes: 35 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,38 @@ 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,
publicationId: publication.id,
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}`,
Expand All @@ -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;
Expand Down Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -218,7 +236,11 @@ const PublishPost = (payload) => {
id
slug
title
subtitle
url
readTimeInMinutes
tags {
name
}
author {
username
}
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -52,13 +51,12 @@
"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": [
"prettier --write",
"eslint --max-warnings 5"
]
}
}
}
47 changes: 33 additions & 14 deletions src/controller/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axios from "axios";
import lodash from "lodash";
import {
MyPublications,
PublishPost,
Expand All @@ -12,17 +11,31 @@ export const publishBlog = async (
hashnode_key: string,
article: any,
host: string
): Promise<any> => {
): 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,
Expand All @@ -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}`,
Expand All @@ -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),
},
};
}
};

Expand Down
10 changes: 9 additions & 1 deletion src/libs/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// ******************************************************************************************* //
// ******************* QUERY ************************ //
// ******************************************************************************************* //

export const Me = () => {
return {
operationName: "Me",
Expand Down Expand Up @@ -84,7 +88,11 @@ export const PublishPost = (payload: PublishPostProps) => {
id
slug
title
subtitle
url
readTimeInMinutes
tags {
name
}
author {
username
}
Expand Down
9 changes: 9 additions & 0 deletions src/libs/axios.ts
Original file line number Diff line number Diff line change
@@ -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 };
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 164102d

Please sign in to comment.