Skip to content

Commit

Permalink
chore: rename types
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Oct 27, 2024
1 parent 6644649 commit bdf21f3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 59 deletions.
22 changes: 9 additions & 13 deletions src/async.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { getProviderForUrl } from "./detect.ts";
import type {
AllProviderOperations,
AllProviderOptions,
ProviderOperations,
ProviderOptions,
} from "./providers/types.ts";
import type { ProviderOperations, ProviderOptions } from "./providers/types.ts";
import type { ProviderModule } from "./providers/types.ts";
import type {
ImageCdn,
Expand Down Expand Up @@ -109,11 +104,12 @@ export const getTransformerForProvider = async <
*/
export async function transformUrl<TCDN extends ImageCdn = ImageCdn>(
url: string | URL,
{ provider, cdn: cdnOption, fallback, ...operations }: UrlTransformerOptions<
TCDN
>,
providerOperations?: ProviderOperations,
providerOptions?: ProviderOptions,
{ provider, cdn: cdnOption, fallback, ...operations }:
UrlTransformerOptions<
TCDN
>,
providerOperations?: Partial<ProviderOperations>,
providerOptions?: Partial<ProviderOptions>,
): Promise<string | undefined> {
const cdn = provider || cdnOption ||
getProviderForUrl(url) as TCDN || fallback;
Expand All @@ -125,7 +121,7 @@ export async function transformUrl<TCDN extends ImageCdn = ImageCdn>(
const transformer = await getTransformerForProvider(cdn);

return transformer?.(url, {
...operations as AllProviderOperations[TCDN],
...operations as ProviderOperations[TCDN],
...providerOperations?.[cdn],
}, providerOptions?.[cdn] ?? {} as AllProviderOptions[TCDN]);
}, providerOptions?.[cdn] ?? {} as ProviderOptions[TCDN]);
}
10 changes: 5 additions & 5 deletions src/extract.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getProviderForUrl } from "./detect.ts";
import type {
AllProviderOperations,
AllProviderOptions,
ProviderOperations,
ProviderOptions,
URLExtractorMap,
} from "./providers/types.ts";
import type { ImageCdn, ParseURLResult, URLExtractor } from "./types.ts";
Expand Down Expand Up @@ -91,7 +91,7 @@ export const parseUrl = <
>(
url: string | URL,
cdn?: TCDN,
options?: AllProviderOptions[TCDN],
options?: ProviderOptions[TCDN],
): ParseURLResult<TCDN> => {
const detectedCdn = cdn || getProviderForUrl(url) as TCDN;
if (!detectedCdn) {
Expand All @@ -103,8 +103,8 @@ export const parseUrl = <
if (!parser) {
return {
src: url.toString(),
operations: {} as AllProviderOperations[TCDN],
options: {} as AllProviderOptions[TCDN],
operations: {} as ProviderOperations[TCDN],
options: {} as ProviderOptions[TCDN],
cdn: detectedCdn,
};
}
Expand Down
9 changes: 3 additions & 6 deletions src/providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import type { UploadcareOperations, UploadcareOptions } from "./uploadcare.ts";
import type { VercelOperations, VercelOptions } from "./vercel.ts";
import type { WordPressOperations } from "./wordpress.ts";

export interface AllProviderOperations {
export interface ProviderOperations {
astro: AstroOperations;
"builder.io": BuilderOperations;
bunny: BunnyOperations;
Expand Down Expand Up @@ -64,7 +64,7 @@ export interface AllProviderOperations {
wordpress: WordPressOperations;
}

export interface AllProviderOptions {
export interface ProviderOptions {
astro: AstroOptions;
"builder.io": undefined;
bunny: undefined;
Expand Down Expand Up @@ -100,13 +100,10 @@ export type URLGeneratorMap = {
[K in ImageCdn]: URLGenerator<K>;
};

export type ProviderTransformerMap = {
export type URLTransformerMap = {
[K in ImageCdn]: URLTransformer<K>;
};

export type ProviderOptions = Partial<AllProviderOptions>;
export type ProviderOperations = Partial<AllProviderOperations>;

export type ProviderModule<
TCDN extends ImageCdn,
> = {
Expand Down
21 changes: 10 additions & 11 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ import {
type UrlTransformerOptions,
} from "./types.ts";
import type {
AllProviderOperations,
AllProviderOptions,
ProviderOperations,
ProviderOptions,
ProviderTransformerMap,
URLTransformerMap,
} from "./providers/types.ts";

const transformerMap: ProviderTransformerMap = {
const transformerMap: URLTransformerMap = {
astro,
"builder.io": builderio,
bunny,
Expand Down Expand Up @@ -83,11 +81,12 @@ export function getTransformerForCdn<TCDN extends ImageCdn>(
*/
export function transformUrl<TCDN extends ImageCdn = ImageCdn>(
url: string | URL,
{ provider, cdn: cdnOption, fallback, ...operations }: UrlTransformerOptions<
TCDN
>,
providerOperations?: ProviderOperations,
providerOptions?: ProviderOptions,
{ provider, cdn: cdnOption, fallback, ...operations }:
UrlTransformerOptions<
TCDN
>,
providerOperations?: Partial<ProviderOperations>,
providerOptions?: Partial<ProviderOptions>,
): string | undefined {
const cdn = provider || cdnOption ||
getProviderForUrl(url) as TCDN || fallback;
Expand All @@ -97,7 +96,7 @@ export function transformUrl<TCDN extends ImageCdn = ImageCdn>(
}

return getTransformerForCdn(cdn)?.(url, {
...operations as AllProviderOperations[TCDN],
...operations as ProviderOperations[TCDN],
...providerOperations?.[cdn],
}, providerOptions?.[cdn] ?? {} as AllProviderOptions[TCDN]);
}, providerOptions?.[cdn] ?? {} as ProviderOptions[TCDN]);
}
33 changes: 15 additions & 18 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type {
AllProviderOperations,
AllProviderOptions,
} from "./providers/types.ts";
import type { ProviderOperations, ProviderOptions } from "./providers/types.ts";

/**
* Options to transform an image URL
*/
export interface UrlTransformerOptions<TCDN extends ImageCdn = ImageCdn>
extends
Pick<
AllProviderOperations[TCDN],
ProviderOperations[TCDN],
"width" | "height" | "format" | "quality"
> {
/** Specify a provider rather than auto-detecting */
Expand Down Expand Up @@ -122,38 +119,38 @@ export interface ProviderConfig<

export type URLGenerator<
TCDN extends ImageCdn = ImageCdn,
> = AllProviderOptions[TCDN] extends undefined
? (src: string | URL, operations: AllProviderOperations[TCDN]) => string
> = ProviderOptions[TCDN] extends undefined
? (src: string | URL, operations: ProviderOperations[TCDN]) => string
: (
src: string | URL,
operations: AllProviderOperations[TCDN],
options?: AllProviderOptions[TCDN],
operations: ProviderOperations[TCDN],
options?: ProviderOptions[TCDN],
) => string;

export type URLTransformer<
TCDN extends ImageCdn = ImageCdn,
> = AllProviderOptions[TCDN] extends undefined
? (src: string | URL, operations: AllProviderOperations[TCDN]) => string
> = ProviderOptions[TCDN] extends undefined
? (src: string | URL, operations: ProviderOperations[TCDN]) => string
: (
src: string | URL,
operations: AllProviderOperations[TCDN],
options?: AllProviderOptions[TCDN],
operations: ProviderOperations[TCDN],
options?: ProviderOptions[TCDN],
) => string;

export type URLExtractor<
TCDN extends ImageCdn = ImageCdn,
> = (
url: string | URL,
options?: AllProviderOptions[TCDN],
options?: ProviderOptions[TCDN],
) =>
| (AllProviderOptions[TCDN] extends undefined ? {
operations: AllProviderOperations[TCDN];
| (ProviderOptions[TCDN] extends undefined ? {
operations: ProviderOperations[TCDN];
src: string;
}
: {
operations: AllProviderOperations[TCDN];
operations: ProviderOperations[TCDN];
src: string;
options: AllProviderOptions[TCDN];
options: ProviderOptions[TCDN];
})
| null;

Expand Down
9 changes: 3 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type {
AllProviderOperations,
AllProviderOptions,
} from "./providers/types.ts";
import type { ProviderOperations, ProviderOptions } from "./providers/types.ts";
import {
type ImageCdn,
OperationFormatter,
Expand Down Expand Up @@ -378,8 +375,8 @@ export function createExtractAndGenerate<
): URLTransformer<TCDN> {
return ((
src: string | URL,
operations: AllProviderOperations[TCDN],
options?: AllProviderOptions[TCDN],
operations: ProviderOperations[TCDN],
options?: ProviderOptions[TCDN],
) => {
const base = extract(src, options);
if (!base) {
Expand Down

0 comments on commit bdf21f3

Please sign in to comment.