Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] Remove the utils-legacy package #5927

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages-next/fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"@keystone-next/admin-ui-utils": "^5.0.2",
"@keystone-next/keystone": "^20.0.0",
"@keystone-next/types": "^20.0.0",
"@keystone-next/utils-legacy": "^12.0.0",
"@keystone-ui/button": "^5.0.0",
"@keystone-ui/core": "^3.1.0",
"@keystone-ui/fields": "^4.1.1",
Expand Down
4 changes: 3 additions & 1 deletion packages-next/fields/src/types/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
KeystoneContext,
FileData,
FieldDefaultValue,
AssetMode,
} from '@keystone-next/types';
import { getFileRef } from '@keystone-next/utils-legacy';
import { FileUpload } from 'graphql-upload';
import { resolveView } from '../../resolve-view';

Expand All @@ -23,6 +23,8 @@ const FileFieldInput = schema.inputObject({
fields: { upload: schema.arg({ type: schema.Upload }), ref: schema.arg({ type: schema.String }) },
});

const getFileRef = (mode: AssetMode, name: string) => `${mode}:file:${name}`;

type FileFieldInputType =
| undefined
| null
Expand Down
2 changes: 1 addition & 1 deletion packages-next/fields/src/types/file/views/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { useToasts } from '@keystone-ui/toast';
import { FieldContainer, FieldLabel } from '@keystone-ui/fields';

import { TextInput } from '@keystone-ui/fields';
import { parseFileRef } from '@keystone-next/utils-legacy';
import { Pill } from '@keystone-ui/pill';
import { Button } from '@keystone-ui/button';
import { FieldProps } from '@keystone-next/types';
import { parseFileRef } from '@keystone-next/keystone';

import { FileValue } from './index';

Expand Down
7 changes: 6 additions & 1 deletion packages-next/fields/src/types/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
ImageExtension,
KeystoneContext,
schema,
AssetMode,
} from '@keystone-next/types';
import { getImageRef, SUPPORTED_IMAGE_EXTENSIONS } from '@keystone-next/utils-legacy';
import { FileUpload } from 'graphql-upload';
import { resolveView } from '../../resolve-view';

Expand All @@ -19,6 +19,8 @@ export type ImageFieldConfig<TGeneratedListTypes extends BaseGeneratedListTypes>
isRequired?: boolean;
};

const SUPPORTED_IMAGE_EXTENSIONS = ['jpg', 'png', 'webp', 'gif'];

const ImageExtensionEnum = schema.enum({
name: 'ImageExtension',
values: schema.enumValues(SUPPORTED_IMAGE_EXTENSIONS),
Expand All @@ -29,6 +31,9 @@ const ImageFieldInput = schema.inputObject({
fields: { upload: schema.arg({ type: schema.Upload }), ref: schema.arg({ type: schema.String }) },
});

const getImageRef = (mode: AssetMode, id: string, extension: ImageExtension) =>
`${mode}:image:${id}.${extension}`;

const imageOutputFields = schema.fields<ImageData>()({
id: schema.field({ type: schema.nonNull(schema.ID) }),
filesize: schema.field({ type: schema.nonNull(schema.Int) }),
Expand Down
2 changes: 1 addition & 1 deletion packages-next/fields/src/types/image/views/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { Fragment, ReactNode, RefObject, useEffect, useMemo, useRef, useState }
import { jsx, Stack, useTheme, Text, VisuallyHidden } from '@keystone-ui/core';
import { useToasts } from '@keystone-ui/toast';
import { TextInput } from '@keystone-ui/fields';
import { parseImageRef } from '@keystone-next/utils-legacy';

import { FieldContainer, FieldLabel } from '@keystone-ui/fields';
import { Pill } from '@keystone-ui/pill';
import { Button } from '@keystone-ui/button';
import { FieldProps } from '@keystone-next/types';
import { parseImageRef } from '@keystone-next/keystone';
import { ImageValue } from './index';

function useObjectURL(fileData: File | undefined) {
Expand Down
1 change: 0 additions & 1 deletion packages-next/keystone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@keystone-next/admin-ui-utils": "^5.0.2",
"@keystone-next/fields": "^11.0.0",
"@keystone-next/types": "^20.0.0",
"@keystone-next/utils-legacy": "^12.0.0",
"@keystone-ui/button": "^5.0.0",
"@keystone-ui/core": "^3.1.0",
"@keystone-ui/fields": "^4.1.1",
Expand Down
2 changes: 2 additions & 0 deletions packages-next/keystone/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export { createSystem } from './lib/createSystem';
export { createExpressServer } from './lib/server/createExpressServer';
export { initConfig } from './lib/config/initConfig';
export { createApolloServerMicro } from './lib/server/createApolloServer';
export { parseImageRef } from './lib/context/createImagesContext';
export { parseFileRef } from './lib/context/createFilesContext';
20 changes: 18 additions & 2 deletions packages-next/keystone/src/lib/context/createFilesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import path from 'path';
import crypto from 'crypto';
import { pipeline } from 'stream';
import filenamify from 'filenamify';
import { KeystoneConfig, FilesContext } from '@keystone-next/types';
import { KeystoneConfig, FilesContext, AssetMode } from '@keystone-next/types';
import fs from 'fs-extra';

import { parseFileRef, isLocalAsset, isKeystoneCloudAsset } from '@keystone-next/utils-legacy';
import slugify from '@sindresorhus/slugify';
import {
buildKeystoneCloudFileSrc,
Expand All @@ -16,6 +15,23 @@ import {
const DEFAULT_BASE_URL = '/files';
const DEFAULT_STORAGE_PATH = './public/files';

const FILEREGEX = /^(local|keystone-cloud):file:([^\\\/:\n]+)/;

export const parseFileRef = (ref: string) => {
const match = ref.match(FILEREGEX);
if (match) {
const [, mode, filename] = match;
return {
mode: mode as AssetMode,
filename: filename as string,
};
}
return undefined;
};

const isLocalAsset = (mode: AssetMode) => mode === 'local';
const isKeystoneCloudAsset = (mode: AssetMode) => mode === 'keystone-cloud';

const defaultTransformer = (str: string) => slugify(str);

const generateSafeFilename = (
Expand Down
28 changes: 26 additions & 2 deletions packages-next/keystone/src/lib/context/createImagesContext.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import path from 'path';
import { KeystoneConfig, ImagesContext, ImageMetadata } from '@keystone-next/types';
import {
KeystoneConfig,
ImagesContext,
ImageMetadata,
AssetMode,
ImageExtension,
} from '@keystone-next/types';
import { v4 as uuid } from 'uuid';
import fs from 'fs-extra';
import fromBuffer from 'image-type';
import imageSize from 'image-size';
import { parseImageRef, isLocalAsset, isKeystoneCloudAsset } from '@keystone-next/utils-legacy';
import {
buildKeystoneCloudImageSrc,
getImageMetadataFromKeystoneCloud,
Expand All @@ -14,6 +19,25 @@ import {
const DEFAULT_BASE_URL = '/images';
const DEFAULT_STORAGE_PATH = './public/images';

const isLocalAsset = (mode: AssetMode) => mode === 'local';
const isKeystoneCloudAsset = (mode: AssetMode) => mode === 'keystone-cloud';

const IMAGEREGEX = /^(local|keystone-cloud):image:([^\\\/:\n]+)\.(gif|jpg|png|webp)$/;
export const parseImageRef = (
ref: string
): { mode: AssetMode; id: string; extension: ImageExtension } | undefined => {
const match = ref.match(IMAGEREGEX);
if (match) {
const [, mode, id, ext] = match;
return {
mode: mode as AssetMode,
id,
extension: ext as ImageExtension,
};
}
return undefined;
};

const getImageMetadataFromBuffer = async (buffer: Buffer): Promise<ImageMetadata> => {
const filesize = buffer.length;
const fileType = fromBuffer(buffer);
Expand Down
2 changes: 0 additions & 2 deletions packages/utils/.npmignore

This file was deleted.

Loading