Skip to content

Commit

Permalink
chore: rework
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriocomo committed Feb 10, 2025
1 parent 526804d commit 72fbac2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
26 changes: 9 additions & 17 deletions src/app/oembed/oembed-service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import { ConsumerRequest, ProviderResponse } from './oembed-models';
import { findEndpointUrlByReqUrl } from './providers';
import { OEmbedUrlBuilder } from './oembed-url-builder';
import { findOEmbedEndpointUrlByReqUrl } from './providers';

const getOEmbed = async <T extends ProviderResponse>(req: ConsumerRequest): Promise<T> => {
const matchedUrl = findEndpointUrlByReqUrl(req.url);
const getOEmbed = async <T extends ProviderResponse>(request: ConsumerRequest): Promise<T> => {
const oembedEndpoint = findOEmbedEndpointUrlByReqUrl(request.url);

if (!matchedUrl) {
if (!oembedEndpoint) {
throw new Error('no matching schema')
}

const optionalQueryParamsList = [];

if (req.maxheight) {
optionalQueryParamsList.push(['maxheight', req.maxheight]);
}

if (req.maxwidth) {
optionalQueryParamsList.push(['maxwidth', req.maxwidth]);
}

const optionalQueryParams = optionalQueryParamsList.reduce((p, c) => `${p}&${c[0]}=${c[1]}`, '')

const url = `${matchedUrl}?url=${req.url}&format=json${optionalQueryParams}`; console.log(url)
const url = new OEmbedUrlBuilder()
.withOEmbedEndpoint(oembedEndpoint)
.withRequest(request)
.build()

const response = await fetch(url)

Expand Down
38 changes: 38 additions & 0 deletions src/app/oembed/oembed-url-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ConsumerRequest } from "./oembed-models";

class OEmbedUrlBuilder {
#oembedEndpoint?: string;
#request?: ConsumerRequest;

withOEmbedEndpoint(oembedEndpoint: string) {
this.#oembedEndpoint = oembedEndpoint;
return this;
}

withRequest(request: ConsumerRequest) {
this.#request = request;
return this;
}

build() {
if (!this.#request || !this.#oembedEndpoint) {
throw new Error('Missing parameter');
}

const optionalQueryParamsList = [];

if (this.#request.maxheight) {
optionalQueryParamsList.push(['maxheight', this.#request.maxheight]);
}

if (this.#request.maxwidth) {
optionalQueryParamsList.push(['maxwidth', this.#request.maxwidth]);
}

const optionalQueryParams = optionalQueryParamsList.reduce((p, c) => `${p}&${c[0]}=${c[1]}`, '')

return `${this.#oembedEndpoint}?url=${this.#request.url}&format=json${optionalQueryParams}`;
}
}

export { OEmbedUrlBuilder };
4 changes: 2 additions & 2 deletions src/app/oembed/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { findEndpointUrlByReqUrl } from './oembed-provider-service'
import { findOEmbedEndpointUrlByReqUrl } from './oembed-provider-service'

export { findEndpointUrlByReqUrl }
export { findOEmbedEndpointUrlByReqUrl }
4 changes: 2 additions & 2 deletions src/app/oembed/providers/oembed-provider-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function matchesSchemas(url: string, schemas: string[]): boolean {
});
}

function findEndpointUrlByReqUrl(reqUrl: string): string | undefined {
function findOEmbedEndpointUrlByReqUrl(reqUrl: string): string | undefined {
for (const endpoint of ENDPOINTS) {
if (endpoint.schemes?.some(scheme => matchesSchemas(reqUrl, [scheme]))) {
return endpoint.url;
Expand All @@ -18,4 +18,4 @@ function findEndpointUrlByReqUrl(reqUrl: string): string | undefined {
}


export { findEndpointUrlByReqUrl };
export { findOEmbedEndpointUrlByReqUrl };

0 comments on commit 72fbac2

Please sign in to comment.