Skip to content

Commit

Permalink
refactor(syndicator-twitter): use getCanonicalUrl from util package
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Jul 4, 2023
1 parent d8c8688 commit af6c63c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 38 deletions.
6 changes: 3 additions & 3 deletions packages/syndicator-twitter/lib/twitter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable camelcase */
import { Buffer } from "node:buffer";
import { isSameOrigin } from "@indiekit/util";
import { getCanonicalUrl, isSameOrigin } from "@indiekit/util";
import { fetch } from "undici";
import Twitter from "twitter-lite";
import { createStatus, getAbsoluteUrl, getStatusIdFromUrl } from "./utils.js";
import { createStatus, getStatusIdFromUrl } from "./utils.js";

export const twitter = (options) => ({
client: (subdomain = "api") =>
Expand Down Expand Up @@ -86,7 +86,7 @@ export const twitter = (options) => ({
}

try {
const mediaUrl = getAbsoluteUrl(url, me);
const mediaUrl = getCanonicalUrl(url, me);
const response = await fetch(mediaUrl);

if (!response.ok) {
Expand Down
15 changes: 0 additions & 15 deletions packages/syndicator-twitter/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,6 @@ export const createStatus = (properties, mediaIds = null) => {
return parameters;
};

/**
* Get absolute URL
* @param {string} string - URL or path
* @param {string} me - Publication URL
* @returns {string} Absolute URL
*/
export const getAbsoluteUrl = (string, me) => {
try {
return new URL(string).href;
} catch {
const absoluteUrl = path.posix.join(me, string);
return new URL(absoluteUrl).href;
}
};

/**
* Get status ID from Twitter status URL
* @param {string} url - Twitter status URL
Expand Down
8 changes: 4 additions & 4 deletions packages/syndicator-twitter/tests/unit/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ test("Throws error fetching media to upload", async (t) => {
await t.throwsAsync(
twitter(t.context.options).uploadMedia(
t.context.media("image.jpg"),
t.context.publication
t.context.publication.me
),
{
message: "Not Found",
Expand All @@ -157,7 +157,7 @@ test("Uploads media and returns a media id", async (t) => {

const result = await twitter(t.context.options).uploadMedia(
t.context.media("photo1.jpg"),
t.context.publication
t.context.publication.me
);

t.is(result, "1234567890987654321");
Expand All @@ -173,7 +173,7 @@ test("Throws error uploading media", async (t) => {
await t.throwsAsync(
twitter(t.context.options).uploadMedia(
t.context.media("photo2.jpg"),
t.context.publication
t.context.publication.me
),
{
message: /Not found/,
Expand All @@ -184,7 +184,7 @@ test("Throws error uploading media", async (t) => {
test("Returns false passing an object to media upload function", async (t) => {
const result = await twitter(t.context.options).uploadMedia(
{ foo: "bar" },
t.context.publication
t.context.publication.me
);

t.falsy(result);
Expand Down
16 changes: 0 additions & 16 deletions packages/syndicator-twitter/tests/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { IndiekitError } from "@indiekit/error";
import { getFixture } from "@indiekit-test/fixtures";
import {
createStatus,
getAbsoluteUrl,
getStatusIdFromUrl,
htmlToStatusText,
} from "../../lib/utils.js";
Expand Down Expand Up @@ -104,21 +103,6 @@ test("Creates a status with a photo", (t) => {
t.is(result.media_ids, "1,2,3,4");
});

test("Gets absolute URL", (t) => {
const result = getAbsoluteUrl(
`${t.context.me}/media/photo.jpg`,
t.context.me
);

t.is(result, `${t.context.me}/media/photo.jpg`);
});

test("Gets absolute URL by prepending publication URL", (t) => {
const result = getAbsoluteUrl("/media/photo.jpg", t.context.me);

t.is(result, `${t.context.me}/media/photo.jpg`);
});

test("Gets status ID from Twitter permalink", (t) => {
const result = getStatusIdFromUrl(
"https://twitter.com/paulrobertlloyd/status/1341502435760680961"
Expand Down

0 comments on commit af6c63c

Please sign in to comment.