Skip to content

Commit

Permalink
chore: revert url changes (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge authored Jan 13, 2025
1 parent aab8ab6 commit e7ccec9
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 54 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-shrimps-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"uploadthing": patch
---

chore: add new `ufsUrl` field with `https://APP_ID.ufs.sh/f/FILE_KEY` url. deprecated `url` and `appUrl` fields.
4 changes: 4 additions & 0 deletions packages/uploadthing/src/_internal/deprecations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const logDeprecationWarning = (message: string) => {
// eslint-disable-next-line no-console
console.warn(`⚠️ [uploadthing][deprecated] ${message}`);
};
17 changes: 16 additions & 1 deletion packages/uploadthing/src/_internal/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import * as pkgJson from "../../package.json";
import type { FileRouter, RouteHandlerOptions } from "../types";
import { IngestUrl, IsDevelopment, UTToken } from "./config";
import { logDeprecationWarning } from "./deprecations";
import { formatError } from "./error-formatter";
import { handleJsonLineStream } from "./jsonl";
import { logHttpClientError, logHttpClientResponse } from "./logger";
Expand Down Expand Up @@ -332,7 +333,21 @@ const handleCallbackRequest = (opts: {
try: async () =>
uploadable.onUploadComplete({
...adapterArgs,
file: requestInput.file,
file: {
...requestInput.file,
get url() {
logDeprecationWarning(
"`file.url` is deprecated and will be removed in uploadthing v9. Use `file.ufsUrl` instead.",
);
return requestInput.file.url;
},
get appUrl() {
logDeprecationWarning(
"`file.appUrl` is deprecated and will be removed in uploadthing v9. Use `file.ufsUrl` instead.",
);
return requestInput.file.appUrl;
},
},
metadata: requestInput.metadata,
}) as Promise<unknown>,
catch: (error) =>
Expand Down
11 changes: 7 additions & 4 deletions packages/uploadthing/src/_internal/shared-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,24 @@ export class FileUploadDataWithCustomId extends FileUploadData.extend<FileUpload
/**
* When files are uploaded, we get back
* - a key
* - a direct URL for the file
* - an app-specific URL for the file (useful for scoping eg. for optimization allowed origins)
* - URLs for the file
* - the hash (md5-hex) of the uploaded file's contents
*/
export class UploadedFileData extends FileUploadDataWithCustomId.extend<UploadedFileData>(
"UploadedFileData",
)({
key: S.String,
/**
* @deprecated
* This field will be removed in uploadthing v9. Use `ufsUrl` instead.
*/
url: S.String,
/**
* @deprecated
* This field is now an alias for `url`.
* This field will be removed in uploadthing v9.
* This field will be removed in uploadthing v9. Use `ufsUrl` instead.
*/
appUrl: S.String,
ufsUrl: S.String,
fileHash: S.String,
}) {}

Expand Down
8 changes: 6 additions & 2 deletions packages/uploadthing/src/_internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,15 @@ export type UTEvents = {
* Result from the PUT request to the UploadThing Ingest server
*/
export type UploadPutResult<TServerOutput = unknown> = {
ufsUrl: string;
/**
* @deprecated
* This field will be removed in uploadthing v9. Use `ufsUrl` instead.
*/
url: string;
/**
* @deprecated
* This field is now an alias for `url`.
* This field will be removed in uploadthing v9.
* This field will be removed in uploadthing v9. Use `ufsUrl` instead.
*/
appUrl: string;
fileHash: string;
Expand Down
16 changes: 14 additions & 2 deletions packages/uploadthing/src/_internal/upload-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
NewPresignedUrl,
UploadFilesOptions,
} from "../types";
import { logDeprecationWarning } from "./deprecations";
import type { UploadPutResult } from "./types";
import { createUTReporter } from "./ut-reporter";

Expand Down Expand Up @@ -138,8 +139,19 @@ export const uploadFile = <
key: presigned.key,
lastModified: file.lastModified,
serverData: uploadResponse.serverData,
url: uploadResponse.url,
appUrl: uploadResponse.appUrl,
get url() {
logDeprecationWarning(
"`file.url` is deprecated and will be removed in uploadthing v9. Use `file.ufsUrl` instead.",
);
return uploadResponse.url;
},
get appUrl() {
logDeprecationWarning(
"`file.appUrl` is deprecated and will be removed in uploadthing v9. Use `file.ufsUrl` instead.",
);
return uploadResponse.appUrl;
},
ufsUrl: uploadResponse.ufsUrl,
customId: presigned.customId,
type: file.type,
fileHash: uploadResponse.fileHash,
Expand Down
18 changes: 17 additions & 1 deletion packages/uploadthing/src/_internal/upload-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UploadThingError } from "@uploadthing/shared";

import { version } from "../../package.json";
import type { FileEsque } from "../sdk/types";
import { logDeprecationWarning } from "./deprecations";
import { logHttpClientError } from "./logger";
import type { UploadPutResult } from "./types";

Expand Down Expand Up @@ -42,5 +43,20 @@ export const uploadWithoutProgress = (
yield* Effect.logDebug(`File ${file.name} uploaded successfully`).pipe(
Effect.annotateLogs("json", json),
);
return json;

return {
...json,
get url() {
logDeprecationWarning(
"`file.url` is deprecated and will be removed in uploadthing v9. Use `file.ufsUrl` instead.",
);
return json.url;
},
get appUrl() {
logDeprecationWarning(
"`file.appUrl` is deprecated and will be removed in uploadthing v9. Use `file.ufsUrl` instead.",
);
return json.appUrl;
},
};
});
1 change: 1 addition & 0 deletions packages/uploadthing/src/sdk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const uploadFile = (
key: presigned.key,
url: response.url,
appUrl: response.appUrl,
ufsUrl: response.ufsUrl,
lastModified: file.lastModified ?? Date.now(),
name: file.name,
size: file.size,
Expand Down
16 changes: 12 additions & 4 deletions packages/uploadthing/test/__test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export const API_URL =
typeof process !== "undefined" && process.env.UPLOADTHING_API_URL
? process.env.UPLOADTHING_API_URL
: "https://api.uploadthing.com";
export const UTFS_URL =
typeof process !== "undefined" && process.env.UPLOADTHING_API_URL
? "https://staging.utfs.io"
: "https://utfs.io";
export const UFS_HOST =
typeof process !== "undefined" && process.env.UPLOADTHING_API_URL
? "utf-staging.com"
Expand All @@ -40,7 +44,10 @@ export const INGEST_URL =
? "https://fra1.ingest.ut-staging.com"
: "https://fra1.ingest.uploadthing.com";

export const fileUrlPattern = (appId = testToken.decoded.appId) =>
export const appUrlPattern = (appId = testToken.decoded.appId) =>
new RegExp(`^${UTFS_URL}/a/${appId}/.+$`);
export const fileUrlPattern = () => new RegExp(`^${UTFS_URL}/f/.+$`);
export const ufsUrlPattern = (appId = testToken.decoded.appId) =>
new RegExp(`^https://${appId}.${UFS_HOST}/f/.+$`);

export const createApiUrl = (slug: string, action?: typeof ActionType.Type) => {
Expand Down Expand Up @@ -98,8 +105,9 @@ export const handlers = [
await callRequestSpy(request);
const appId = new URLSearchParams(request.url).get("x-ut-identifier");
return HttpResponse.json<UploadPutResult>({
url: `https://${appId}.${UFS_HOST}/f/${params.key}`,
appUrl: `https://${appId}.${UFS_HOST}/f/${params.key}`,
url: `${UTFS_URL}/f/${params.key}`,
appUrl: `${UTFS_URL}/a/${appId}/${params.key}`,
ufsUrl: `https://${appId}.${UFS_HOST}/f/${params.key}`,
serverData: null,
fileHash: Array.from(new Uint8Array(await request.arrayBuffer()))
.map((b) => b.toString(16).padStart(2, "0"))
Expand All @@ -113,7 +121,7 @@ export const handlers = [
http.post(`${API_URL}/v6/requestFileAccess`, async ({ request }) => {
await callRequestSpy(request);
return HttpResponse.json({
url: `https://{APP_ID}.${UFS_HOST}/f/someFileKey?x-some-amz=query-param`,
url: `${UTFS_URL}/f/someFileKey?x-some-amz=query-param`,
});
}),
http.post(`${API_URL}/v6/updateACL`, async ({ request }) => {
Expand Down
11 changes: 8 additions & 3 deletions packages/uploadthing/test/client.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import type {
GenerateUploaderOptions,
} from "../src/types";
import {
appUrlPattern,
doNotExecute,
fileUrlPattern,
handlers,
middlewareMock,
onErrorMock,
requestSpy,
testToken,
ufsUrlPattern,
uploadCompleteMock,
} from "./__test-helpers";

Expand Down Expand Up @@ -141,7 +143,8 @@ describe("uploadFiles", () => {
lastModified: expect.any(Number),
key: expect.stringMatching(/.+/),
url: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(appUrlPattern()),
ufsUrl: expect.stringMatching(ufsUrlPattern()),
fileHash: expect.any(String),
},
]);
Expand Down Expand Up @@ -185,7 +188,8 @@ describe("uploadFiles", () => {
lastModified: expect.any(Number),
key: expect.stringMatching(/.+/),
url: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(appUrlPattern()),
ufsUrl: expect.stringMatching(ufsUrlPattern()),
fileHash: expect.any(String),
},
]);
Expand Down Expand Up @@ -220,7 +224,8 @@ describe("uploadFiles", () => {
lastModified: expect.any(Number),
key: expect.stringMatching(/.+/),
url: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(appUrlPattern()),
ufsUrl: expect.stringMatching(ufsUrlPattern()),
fileHash: expect.any(String),
},
]);
Expand Down
21 changes: 13 additions & 8 deletions packages/uploadthing/test/request-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
testToken,
UFS_HOST,
uploadCompleteMock,
UTFS_URL,
} from "./__test-helpers";

const f = createUploadthing({
Expand Down Expand Up @@ -410,8 +411,9 @@ describe(".onUploadComplete()", () => {
status: "uploaded",
metadata: {},
file: new UploadedFileData({
url: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
appUrl: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
url: `${UTFS_URL}/f/some-random-key.png`,
appUrl: `${UTFS_URL}/a/${testToken.decoded.appId}/f/some-random-key.png`,
ufsUrl: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
name: "foo.png",
key: "some-random-key.png",
size: 48,
Expand Down Expand Up @@ -447,8 +449,9 @@ describe(".onUploadComplete()", () => {
name: "foo.png",
size: 48,
type: "image/png",
url: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
appUrl: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
url: `${UTFS_URL}/f/some-random-key.png`,
appUrl: `${UTFS_URL}/a/${testToken.decoded.appId}/f/some-random-key.png`,
ufsUrl: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
fileHash: "some-md5-hash",
},
metadata: {},
Expand All @@ -460,8 +463,9 @@ describe(".onUploadComplete()", () => {
status: "uploaded",
metadata: {},
file: new UploadedFileData({
url: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
appUrl: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
url: `${UTFS_URL}/f/some-random-key.png`,
appUrl: `${UTFS_URL}/a/${testToken.decoded.appId}/f/some-random-key.png`,
ufsUrl: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
name: "foo.png",
key: "some-random-key.png",
size: 48,
Expand Down Expand Up @@ -493,8 +497,9 @@ describe(".onUploadComplete()", () => {
status: "uploaded",
metadata: {},
file: new UploadedFileData({
url: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
appUrl: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
url: `${UTFS_URL}/f/some-random-key.png`,
appUrl: `${UTFS_URL}/a/${testToken.decoded.appId}/f/some-random-key.png`,
ufsUrl: `https://${testToken.decoded.appId}.${UFS_HOST}/f/some-random-key.png`,
name: "foo.png",
key: "some-random-key.png",
size: 48,
Expand Down
37 changes: 24 additions & 13 deletions packages/uploadthing/test/sdk.live.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { afterAll, beforeAll, describe, expect, it } from "vitest";

import { UTApi, UTFile } from "../src/sdk";
import { UploadThingToken } from "../src/types";
import { fileUrlPattern, testToken } from "./__test-helpers";
import {
appUrlPattern,
fileUrlPattern,
testToken,
ufsUrlPattern,
} from "./__test-helpers";

const shouldRun =
typeof process.env.UPLOADTHING_TEST_TOKEN === "string" &&
Expand Down Expand Up @@ -64,8 +69,9 @@ describe.runIf(shouldRun)(
name: "foo.txt",
size: 3,
type: "text/plain",
url: expect.stringMatching(fileUrlPattern(appId)),
appUrl: expect.stringMatching(fileUrlPattern(appId)),
url: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(appUrlPattern(appId)),
ufsUrl: expect.stringMatching(ufsUrlPattern(appId)),
fileHash: expect.any(String),
},
error: null,
Expand Down Expand Up @@ -99,8 +105,9 @@ describe.runIf(shouldRun)(
name: "foo.txt",
size: 3,
type: "text/plain",
url: expect.stringMatching(fileUrlPattern(appId)),
appUrl: expect.stringMatching(fileUrlPattern(appId)),
url: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(appUrlPattern(appId)),
ufsUrl: expect.stringMatching(ufsUrlPattern(appId)),
fileHash: expect.any(String),
},
error: null,
Expand Down Expand Up @@ -129,8 +136,9 @@ describe.runIf(shouldRun)(
name: "favicon.ico",
size: expect.any(Number),
type: "image/vnd.microsoft.icon",
url: expect.stringMatching(fileUrlPattern(appId)),
appUrl: expect.stringMatching(fileUrlPattern(appId)),
url: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(appUrlPattern(appId)),
ufsUrl: expect.stringMatching(ufsUrlPattern(appId)),
fileHash: expect.any(String),
},
error: null,
Expand All @@ -153,8 +161,9 @@ describe.runIf(shouldRun)(
name: "bar.txt",
size: 3,
type: "text/plain",
url: expect.stringMatching(fileUrlPattern(appId)),
appUrl: expect.stringMatching(fileUrlPattern(appId)),
url: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(appUrlPattern(appId)),
ufsUrl: expect.stringMatching(ufsUrlPattern(appId)),
fileHash: expect.any(String),
},
error: null,
Expand Down Expand Up @@ -195,8 +204,9 @@ describe.runIf(shouldRun)(
name: "bar.txt",
size: 3,
type: "text/plain",
url: expect.stringMatching(fileUrlPattern(appId)),
appUrl: expect.stringMatching(fileUrlPattern(appId)),
url: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(appUrlPattern(appId)),
ufsUrl: expect.stringMatching(ufsUrlPattern(appId)),
fileHash: expect.any(String),
},
error: null,
Expand Down Expand Up @@ -236,8 +246,9 @@ describe.runIf(shouldRun)(
name: "foo.txt",
size: 3,
type: "text/plain",
url: expect.stringMatching(fileUrlPattern(appId)),
appUrl: expect.stringMatching(fileUrlPattern(appId)),
url: expect.stringMatching(fileUrlPattern()),
appUrl: expect.stringMatching(appUrlPattern(appId)),
ufsUrl: expect.stringMatching(ufsUrlPattern(appId)),
fileHash: expect.any(String),
},
error: null,
Expand Down
Loading

0 comments on commit e7ccec9

Please sign in to comment.