Skip to content

Commit

Permalink
rename to invariant
Browse files Browse the repository at this point in the history
  • Loading branch information
prtcl committed Nov 12, 2024
1 parent 6178146 commit da85585
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
18 changes: 9 additions & 9 deletions convex/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const unarchiveProject = internalMutation({
export const generateUploadUrl = mutation({
args: { token: v.string() },
handler: async (ctx, args) => {
assertUploadToken(args.token);
invariantUploadToken(args.token);

return {
uploadUrl: await ctx.storage.generateUploadUrl(),
Expand Down Expand Up @@ -115,7 +115,7 @@ export const createImage = mutation({
payload: { naturalHeight, naturalWidth, ...restPayload },
token,
} = args;
assertUploadToken(token);
invariantUploadToken(token);

return await ctx.db.insert('images', {
...restPayload,
Expand All @@ -136,7 +136,7 @@ export const attachImagePreview = mutation({
},
handler: async (ctx, args) => {
const { previewImageId, projectId, token } = args;
assertUploadToken(token);
invariantUploadToken(token);

const project = await ctx.db.get(projectId);

Expand All @@ -158,7 +158,7 @@ export const attachImagePreview = mutation({
}

const targetPreviewImage = await ctx.db.get(previewImageId);
assertImageEntity(targetPreviewImage);
invariantImageEntity(targetPreviewImage);

return await ctx.db.patch(project._id, {
previewImageId,
Expand Down Expand Up @@ -222,7 +222,7 @@ export const attachProjectEmbed = internalMutation({
}

const service = detectEmbedService(src);
assertEmbedService(service);
invariantEmbedService(service);

const embedId = await ctx.db.insert('embeds', {
deletedAt: null,
Expand Down Expand Up @@ -257,7 +257,7 @@ export const attachProjectCoverImage = internalMutation({
}

const targetCoverImage = await ctx.db.get(coverImageId);
assertImageEntity(targetCoverImage);
invariantImageEntity(targetCoverImage);

return await ctx.db.patch(projectId, {
coverImageId: targetCoverImage._id,
Expand All @@ -266,7 +266,7 @@ export const attachProjectCoverImage = internalMutation({
},
});

function assertUploadToken(token: unknown): asserts token is string {
function invariantUploadToken(token: unknown): asserts token is string {
if (!process.env.UPLOAD_TOKEN) {
throw new Error('No upload token set!');
}
Expand All @@ -280,7 +280,7 @@ function isEmbedService(value: unknown): value is Services {
return typeof value === 'string' && services.has(value as Services);
}

function assertEmbedService(value: unknown): asserts value is Services {
function invariantEmbedService(value: unknown): asserts value is Services {
if (!isEmbedService(value)) {
throw new Error('Service string is invalid');
}
Expand All @@ -290,7 +290,7 @@ function isImageEntity(value: unknown): value is Doc<'images'> {
return typeof value === 'object' && value !== null && 'storageId' in value;
}

function assertImageEntity(value: unknown): asserts value is Doc<'images'> {
function invariantImageEntity(value: unknown): asserts value is Doc<'images'> {
if (!isImageEntity(value)) {
throw new Error('Image entity is invalid or not found');
}
Expand Down
16 changes: 8 additions & 8 deletions scripts/import-screenshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
import {
type ImagePayload,
type ProjectId,
assertImageDimensions,
assertProjectEntity,
assertProjectId,
assertUploadResponse,
invariantImageDimensions,
invariantProjectEntity,
invariantProjectId,
invariantUploadResponse,
} from './lib/types';

dotenv.config({ path: '.env.local' });
Expand Down Expand Up @@ -62,8 +62,8 @@ async function main(deploymentUrl: string, uploadToken: string) {
for (const filename of imageFiles) {
const projectId = path.parse(filename).name as ProjectId;
const project = fromProjectId.get(projectId);
assertProjectId(projectId);
assertProjectEntity(project);
invariantProjectId(projectId);
invariantProjectEntity(project);

console.log(`Uploading ${filename}`);

Expand All @@ -74,11 +74,11 @@ async function main(deploymentUrl: string, uploadToken: string) {
);

const uploadResponse = await uploadImageFile(uploadUrl, file);
assertUploadResponse(uploadResponse);
invariantUploadResponse(uploadResponse);

const { storageId } = uploadResponse;
const dimensions = await getImageDimensions(file);
assertImageDimensions(dimensions);
invariantImageDimensions(dimensions);

const imagePayload: ImagePayload = {
alt: project.title,
Expand Down
8 changes: 4 additions & 4 deletions scripts/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function isImageDimensions(
);
}

export function assertImageDimensions(
export function invariantImageDimensions(
payload: unknown,
): asserts payload is ImageDimensions {
if (!isImageDimensions(payload)) {
Expand All @@ -37,21 +37,21 @@ export function isUploadResponse(res: unknown): res is UploadResponse {
return typeof res === 'object' && res !== null && 'storageId' in res;
}

export function assertUploadResponse(
export function invariantUploadResponse(
res: unknown,
): asserts res is UploadResponse {
if (!isUploadResponse(res)) {
throw new Error('Response is not an upload response');
}
}

export function assertProjectId(value: unknown): asserts value is ProjectId {
export function invariantProjectId(value: unknown): asserts value is ProjectId {
if (typeof value !== 'string') {
throw new Error('Value is not a product ID');
}
}

export function assertProjectEntity(
export function invariantProjectEntity(
value: unknown,
): asserts value is ProjectEntity {
if (
Expand Down
8 changes: 4 additions & 4 deletions scripts/upload-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from './lib/helpers';
import {
type ImagePayload,
assertImageDimensions,
assertUploadResponse,
invariantImageDimensions,
invariantUploadResponse,
} from './lib/types';

dotenv.config({ path: '.env.local' });
Expand Down Expand Up @@ -57,11 +57,11 @@ async function main(
);

const uploadResponse = await uploadImageFile(uploadUrl, file);
assertUploadResponse(uploadResponse);
invariantUploadResponse(uploadResponse);

const { storageId } = uploadResponse;
const dimensions = await getImageDimensions(file);
assertImageDimensions(dimensions);
invariantImageDimensions(dimensions);

const imagePayload: ImagePayload = {
alt: null,
Expand Down

0 comments on commit da85585

Please sign in to comment.