This commit is contained in:
2026-05-12 07:54:32 +03:00
parent 0faa8b9d2d
commit d49449c30c
187 changed files with 4826 additions and 5884 deletions

View File

@@ -0,0 +1,21 @@
{
"name": "@image-platform/client",
"version": "0.1.0",
"private": true,
"exports": {
".": {
"types": "./src/index.ts",
"require": "./dist/index.js",
"import": "./dist/index.js",
"default": "./dist/index.js"
}
},
"types": "./src/index.ts",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"typecheck": "tsc --noEmit -p tsconfig.json"
},
"devDependencies": {
"typescript": "^6.0.3"
}
}

View File

@@ -0,0 +1,127 @@
export type ImagePlatformFormat = "auto" | "avif" | "jpg" | "png" | "webp"
export type ImagePlatformResize = "fill" | "fit"
export type ImagePlatformRemoteUrlInput = {
baseUrl: string
fit?: ImagePlatformResize
format?: ImagePlatformFormat
height?: number | null
preset: string
project: string
quality?: number | null
sourceBaseUrl?: string
src: string
width?: number | null
}
export type ImagePlatformNextLoaderOptions = {
baseUrl: string
defaultQuality?: number
fit?: ImagePlatformResize
format?: ImagePlatformFormat
preset: string
project: string
sourceBaseUrl?: string
}
export type ImagePlatformNextLoaderParams = {
quality?: number
src: string
width: number
}
export type ImagePlatformOperations = {
fit?: ImagePlatformResize
format?: ImagePlatformFormat
height?: number
quality?: number
width?: number
}
export type ImagePlatformUnpicOptions = {
baseUrl: string
defaultQuality?: number
fit?: ImagePlatformResize
format?: ImagePlatformFormat
preset: string
project: string
sourceBaseUrl?: string
}
export function buildImagePlatformRemoteUrl(input: ImagePlatformRemoteUrlInput) {
const sourceUrl = resolveImagePlatformSourceUrl(input.src, input.sourceBaseUrl)
const url = new URL(`/p/${encodePathSegment(input.project)}/remote/${encodePathSegment(input.preset)}`, input.baseUrl)
url.searchParams.set("src", sourceUrl)
if (input.width) {
url.searchParams.set("w", input.width.toString())
}
if (input.height) {
url.searchParams.set("h", input.height.toString())
}
if (input.quality) {
url.searchParams.set("q", input.quality.toString())
}
if (input.fit) {
url.searchParams.set("fit", input.fit)
}
url.searchParams.set("f", input.format ?? "auto")
return url.toString()
}
export function createImagePlatformNextLoader(options: ImagePlatformNextLoaderOptions) {
return function imagePlatformNextLoader(params: ImagePlatformNextLoaderParams) {
return buildImagePlatformRemoteUrl({
baseUrl: options.baseUrl,
fit: options.fit,
format: options.format,
preset: options.preset,
project: options.project,
quality: params.quality ?? options.defaultQuality,
sourceBaseUrl: options.sourceBaseUrl,
src: params.src,
width: params.width,
})
}
}
export function imagePlatformUnpicTransformer(
src: string,
operations: ImagePlatformOperations,
options: ImagePlatformUnpicOptions,
) {
return buildImagePlatformRemoteUrl({
baseUrl: options.baseUrl,
fit: operations.fit ?? options.fit,
format: operations.format ?? options.format,
height: operations.height,
preset: options.preset,
project: options.project,
quality: operations.quality ?? options.defaultQuality,
sourceBaseUrl: options.sourceBaseUrl,
src,
width: operations.width,
})
}
export function resolveImagePlatformSourceUrl(src: string, sourceBaseUrl?: string) {
try {
return new URL(src).toString()
} catch {
if (!sourceBaseUrl) {
throw new Error("sourceBaseUrl is required for relative image src")
}
return new URL(src, sourceBaseUrl).toString()
}
}
function encodePathSegment(value: string) {
return encodeURIComponent(value).replaceAll("%2D", "-").replaceAll("%5F", "_")
}

View File

@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": false
},
"exclude": ["dist", "node_modules", "**/*.spec.ts"]
}

View File

@@ -0,0 +1,20 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"lib": ["ES2023", "DOM"],
"module": "Node16",
"moduleResolution": "Node16",
"noUncheckedIndexedAccess": true,
"outDir": "./dist",
"rootDir": "./src",
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "ES2023"
},
"include": ["src/**/*.ts"],
"exclude": ["dist", "node_modules"]
}

View File

@@ -0,0 +1,14 @@
CREATE TYPE "public"."project_status" AS ENUM('active', 'disabled');--> statement-breakpoint
CREATE TABLE "image_projects" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"slug" text NOT NULL,
"name" text NOT NULL,
"status" "project_status" DEFAULT 'active' NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "image_assets" ADD COLUMN "project_id" uuid;--> statement-breakpoint
CREATE UNIQUE INDEX "image_projects_slug_idx" ON "image_projects" USING btree ("slug");--> statement-breakpoint
ALTER TABLE "image_assets" ADD CONSTRAINT "image_assets_project_id_image_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."image_projects"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "image_assets_project_id_idx" ON "image_assets" USING btree ("project_id");

View File

@@ -0,0 +1,745 @@
{
"id": "f407b041-197f-4277-8c7d-2b5ce920adc5",
"prevId": "9b706710-b809-4324-8632-634884f75166",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.allowed_image_hosts": {
"name": "allowed_image_hosts",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"hostname": {
"name": "hostname",
"type": "text",
"primaryKey": false,
"notNull": true
},
"enabled": {
"name": "enabled",
"type": "boolean",
"primaryKey": false,
"notNull": true,
"default": true
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"allowed_image_hosts_hostname_idx": {
"name": "allowed_image_hosts_hostname_idx",
"columns": [
{
"expression": "hostname",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.image_asset_versions": {
"name": "image_asset_versions",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"asset_id": {
"name": "asset_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"version": {
"name": "version",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"source_url": {
"name": "source_url",
"type": "text",
"primaryKey": false,
"notNull": true
},
"source_host": {
"name": "source_host",
"type": "text",
"primaryKey": false,
"notNull": true
},
"source_hash": {
"name": "source_hash",
"type": "text",
"primaryKey": false,
"notNull": true
},
"original_s3_key": {
"name": "original_s3_key",
"type": "text",
"primaryKey": false,
"notNull": false
},
"width": {
"name": "width",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"height": {
"name": "height",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"content_type": {
"name": "content_type",
"type": "text",
"primaryKey": false,
"notNull": false
},
"size_bytes": {
"name": "size_bytes",
"type": "bigint",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"image_asset_versions_asset_version_idx": {
"name": "image_asset_versions_asset_version_idx",
"columns": [
{
"expression": "asset_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "version",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
},
"image_asset_versions_source_hash_idx": {
"name": "image_asset_versions_source_hash_idx",
"columns": [
{
"expression": "source_hash",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {
"image_asset_versions_asset_id_image_assets_id_fk": {
"name": "image_asset_versions_asset_id_image_assets_id_fk",
"tableFrom": "image_asset_versions",
"tableTo": "image_assets",
"columnsFrom": [
"asset_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.image_assets": {
"name": "image_assets",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"project_id": {
"name": "project_id",
"type": "uuid",
"primaryKey": false,
"notNull": false
},
"public_id": {
"name": "public_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"current_version": {
"name": "current_version",
"type": "integer",
"primaryKey": false,
"notNull": true,
"default": 1
},
"status": {
"name": "status",
"type": "asset_status",
"typeSchema": "public",
"primaryKey": false,
"notNull": true,
"default": "'active'"
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"image_assets_public_id_idx": {
"name": "image_assets_public_id_idx",
"columns": [
{
"expression": "public_id",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
},
"image_assets_project_id_idx": {
"name": "image_assets_project_id_idx",
"columns": [
{
"expression": "project_id",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {
"image_assets_project_id_image_projects_id_fk": {
"name": "image_assets_project_id_image_projects_id_fk",
"tableFrom": "image_assets",
"tableTo": "image_projects",
"columnsFrom": [
"project_id"
],
"columnsTo": [
"id"
],
"onDelete": "restrict",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.image_projects": {
"name": "image_projects",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"slug": {
"name": "slug",
"type": "text",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"status": {
"name": "status",
"type": "project_status",
"typeSchema": "public",
"primaryKey": false,
"notNull": true,
"default": "'active'"
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"image_projects_slug_idx": {
"name": "image_projects_slug_idx",
"columns": [
{
"expression": "slug",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.image_variants": {
"name": "image_variants",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"asset_id": {
"name": "asset_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"asset_version_id": {
"name": "asset_version_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"asset_version": {
"name": "asset_version",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"preset": {
"name": "preset",
"type": "text",
"primaryKey": false,
"notNull": true
},
"variant_hash": {
"name": "variant_hash",
"type": "text",
"primaryKey": false,
"notNull": true
},
"requested_format": {
"name": "requested_format",
"type": "requested_format",
"typeSchema": "public",
"primaryKey": false,
"notNull": true,
"default": "'auto'"
},
"format": {
"name": "format",
"type": "variant_format",
"typeSchema": "public",
"primaryKey": false,
"notNull": true
},
"width": {
"name": "width",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"height": {
"name": "height",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"resize_mode": {
"name": "resize_mode",
"type": "resize_mode",
"typeSchema": "public",
"primaryKey": false,
"notNull": true,
"default": "'fit'"
},
"quality": {
"name": "quality",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"s3_key": {
"name": "s3_key",
"type": "text",
"primaryKey": false,
"notNull": true
},
"content_type": {
"name": "content_type",
"type": "text",
"primaryKey": false,
"notNull": false
},
"etag": {
"name": "etag",
"type": "text",
"primaryKey": false,
"notNull": false
},
"status": {
"name": "status",
"type": "variant_status",
"typeSchema": "public",
"primaryKey": false,
"notNull": true,
"default": "'pending'"
},
"size_bytes": {
"name": "size_bytes",
"type": "bigint",
"primaryKey": false,
"notNull": false
},
"error": {
"name": "error",
"type": "text",
"primaryKey": false,
"notNull": false
},
"attempt_count": {
"name": "attempt_count",
"type": "integer",
"primaryKey": false,
"notNull": true,
"default": 0
},
"last_accessed_at": {
"name": "last_accessed_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"image_variants_lookup_idx": {
"name": "image_variants_lookup_idx",
"columns": [
{
"expression": "asset_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "asset_version",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "preset",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "width",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "height",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "resize_mode",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "quality",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "format",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
},
"image_variants_s3_key_idx": {
"name": "image_variants_s3_key_idx",
"columns": [
{
"expression": "s3_key",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
},
"image_variants_variant_hash_idx": {
"name": "image_variants_variant_hash_idx",
"columns": [
{
"expression": "variant_hash",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
},
"image_variants_status_idx": {
"name": "image_variants_status_idx",
"columns": [
{
"expression": "status",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {
"image_variants_asset_id_image_assets_id_fk": {
"name": "image_variants_asset_id_image_assets_id_fk",
"tableFrom": "image_variants",
"tableTo": "image_assets",
"columnsFrom": [
"asset_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"image_variants_asset_version_id_image_asset_versions_id_fk": {
"name": "image_variants_asset_version_id_image_asset_versions_id_fk",
"tableFrom": "image_variants",
"tableTo": "image_asset_versions",
"columnsFrom": [
"asset_version_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {
"public.asset_status": {
"name": "asset_status",
"schema": "public",
"values": [
"active",
"disabled",
"deleted"
]
},
"public.project_status": {
"name": "project_status",
"schema": "public",
"values": [
"active",
"disabled"
]
},
"public.requested_format": {
"name": "requested_format",
"schema": "public",
"values": [
"auto",
"avif",
"webp",
"jpg",
"png"
]
},
"public.resize_mode": {
"name": "resize_mode",
"schema": "public",
"values": [
"fit",
"fill"
]
},
"public.variant_format": {
"name": "variant_format",
"schema": "public",
"values": [
"avif",
"webp",
"jpg",
"png"
]
},
"public.variant_status": {
"name": "variant_status",
"schema": "public",
"values": [
"pending",
"processing",
"ready",
"failed"
]
}
},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@@ -15,6 +15,13 @@
"when": 1777973330318,
"tag": "0001_familiar_nextwave",
"breakpoints": true
},
{
"idx": 2,
"version": "7",
"when": 1778007484095,
"tag": "0002_grey_ser_duncan",
"breakpoints": true
}
]
}

View File

@@ -12,6 +12,7 @@ import {
} from "drizzle-orm/pg-core"
export const assetStatusEnum = pgEnum("asset_status", ["active", "disabled", "deleted"])
export const projectStatusEnum = pgEnum("project_status", ["active", "disabled"])
export const variantFormatEnum = pgEnum("variant_format", ["avif", "webp", "jpg", "png"])
export const requestedFormatEnum = pgEnum("requested_format", ["auto", "avif", "webp", "jpg", "png"])
export const resizeModeEnum = pgEnum("resize_mode", ["fit", "fill"])
@@ -34,16 +35,29 @@ export const allowedImageHosts = pgTable(
(table) => [uniqueIndex("allowed_image_hosts_hostname_idx").on(table.hostname)],
)
export const imageProjects = pgTable(
"image_projects",
{
id: uuid("id").primaryKey().defaultRandom(),
slug: text("slug").notNull(),
name: text("name").notNull(),
status: projectStatusEnum("status").notNull().default("active"),
...timestamps,
},
(table) => [uniqueIndex("image_projects_slug_idx").on(table.slug)],
)
export const imageAssets = pgTable(
"image_assets",
{
id: uuid("id").primaryKey().defaultRandom(),
projectId: uuid("project_id").references(() => imageProjects.id, { onDelete: "restrict" }),
publicId: text("public_id").notNull(),
currentVersion: integer("current_version").notNull().default(1),
status: assetStatusEnum("status").notNull().default("active"),
...timestamps,
},
(table) => [uniqueIndex("image_assets_public_id_idx").on(table.publicId)],
(table) => [uniqueIndex("image_assets_public_id_idx").on(table.publicId), index("image_assets_project_id_idx").on(table.projectId)],
)
export const imageAssetVersions = pgTable(