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

Move utils package into main package #6368

Merged
merged 4 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .changeset/shy-tools-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystone-next/keystone': minor
'@keystone-next/utils': major
---

Moved `@keystone-next/utils` to `@keystone-next/keystone/fields/types/image/utils` for image ref related utilities and `@keystone-next/keystone/fields/types/file/utils` for file ref related utilities.
4 changes: 4 additions & 0 deletions packages/keystone/fields/types/file/utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "dist/keystone.cjs.js",
"module": "dist/keystone.esm.js"
}
4 changes: 4 additions & 0 deletions packages/keystone/fields/types/image/utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "dist/keystone.cjs.js",
"module": "dist/keystone.esm.js"
}
4 changes: 2 additions & 2 deletions packages/keystone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@graphql-tools/schema": "^8.1.1",
"@hapi/iron": "^6.0.0",
"@keystone-next/types": "^24.0.0",
"@keystone-next/utils": "^1.0.4",
"@keystone-ui/button": "^5.0.0",
"@keystone-ui/core": "^3.1.1",
"@keystone-ui/fields": "^4.1.2",
Expand Down Expand Up @@ -154,7 +153,8 @@
"admin-ui/utils/index.ts",
"fields/index.ts",
"fields/types/*/views/index.tsx",
"fields/types/relationship/views/RelationshipSelect.tsx"
"fields/types/relationship/views/RelationshipSelect.tsx",
"fields/types/{image,file}/utils.ts"
]
},
"repository": "https://github.com/keystonejs/keystone/tree/master/packages/keystone"
Expand Down
2 changes: 1 addition & 1 deletion packages/keystone/src/fields/types/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
FileData,
FieldDefaultValue,
} from '@keystone-next/types';
import { getFileRef } from '@keystone-next/utils';
import { FileUpload } from 'graphql-upload';
import { resolveView } from '../../resolve-view';
import { getFileRef } from './utils';

export type FileFieldConfig<TGeneratedListTypes extends BaseGeneratedListTypes> =
CommonFieldConfig<TGeneratedListTypes> & {
Expand Down
16 changes: 16 additions & 0 deletions packages/keystone/src/fields/types/file/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AssetMode } from '@keystone-next/types';

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

export const getFileRef = (mode: AssetMode, name: string) => `${mode}:file:${name}`;
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;
};
2 changes: 1 addition & 1 deletion packages/keystone/src/fields/types/file/views/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ 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';
import { Pill } from '@keystone-ui/pill';
import { Button } from '@keystone-ui/button';
import { FieldProps } from '@keystone-next/types';

import { parseFileRef } from '../utils';
import { FileValue } from './index';

export function validateRef({ ref }: { ref: string }) {
Expand Down
2 changes: 1 addition & 1 deletion packages/keystone/src/fields/types/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
KeystoneContext,
schema,
} from '@keystone-next/types';
import { getImageRef, SUPPORTED_IMAGE_EXTENSIONS } from '@keystone-next/utils';
import { FileUpload } from 'graphql-upload';
import { resolveView } from '../../resolve-view';
import { getImageRef, SUPPORTED_IMAGE_EXTENSIONS } from './utils';

export type ImageFieldConfig<TGeneratedListTypes extends BaseGeneratedListTypes> =
CommonFieldConfig<TGeneratedListTypes> & {
Expand Down
23 changes: 23 additions & 0 deletions packages/keystone/src/fields/types/image/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { AssetMode, ImageExtension } from '@keystone-next/types';

const IMAGEREGEX = /^(local|keystone-cloud):image:([^\\\/:\n]+)\.(gif|jpg|png|webp)$/;

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

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

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;
};
2 changes: 1 addition & 1 deletion packages/keystone/src/fields/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';

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 '../utils';
import { ImageValue } from './index';

function useObjectURL(fileData: File | undefined) {
Expand Down
10 changes: 5 additions & 5 deletions packages/keystone/src/lib/context/createFilesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import filenamify from 'filenamify';
import { KeystoneConfig, FilesContext } from '@keystone-next/types';
import fs from 'fs-extra';

import { parseFileRef, isLocalAsset, isKeystoneCloudAsset } from '@keystone-next/utils';
import slugify from '@sindresorhus/slugify';
import { parseFileRef } from '../../fields/types/file/utils';
import {
buildKeystoneCloudFileSrc,
uploadFileToKeystoneCloud,
Expand Down Expand Up @@ -59,13 +59,13 @@ export function createFilesContext(config: KeystoneConfig): FilesContext | undef
restApiEndpoint = '',
} = experimental?.keystoneCloud || {};

if (isLocalAsset(files.upload)) {
if (files.upload === 'local') {
fs.mkdirSync(storagePath, { recursive: true });
}

return {
getSrc: async (mode, filename) => {
if (isKeystoneCloudAsset(mode)) {
if (mode === 'keystone-cloud') {
return await buildKeystoneCloudFileSrc({ apiKey, graphqlApiEndpoint, filename });
}

Expand All @@ -80,7 +80,7 @@ export function createFilesContext(config: KeystoneConfig): FilesContext | undef

const { mode, filename } = fileRef;

if (isKeystoneCloudAsset(mode)) {
if (mode === 'keystone-cloud') {
const { filesize } = await getFileFromKeystoneCloud({
apiKey,
filename,
Expand All @@ -98,7 +98,7 @@ export function createFilesContext(config: KeystoneConfig): FilesContext | undef
const { upload: mode } = files;
const filename = generateSafeFilename(originalFilename, files.transformFilename);

if (isKeystoneCloudAsset(mode)) {
if (mode === 'keystone-cloud') {
const { filesize } = await uploadFileToKeystoneCloud({
apiKey,
stream,
Expand Down
10 changes: 5 additions & 5 deletions packages/keystone/src/lib/context/createImagesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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';
import { parseImageRef } from '../../fields/types/image/utils';
import {
buildKeystoneCloudImageSrc,
getImageMetadataFromKeystoneCloud,
Expand Down Expand Up @@ -54,15 +54,15 @@ export function createImagesContext(config: KeystoneConfig): ImagesContext | und
restApiEndpoint = '',
} = experimental?.keystoneCloud || {};

if (isLocalAsset(images.upload)) {
if (images.upload === 'local') {
fs.mkdirSync(storagePath, { recursive: true });
}

return {
getSrc: async (mode, id, extension) => {
const filename = `${id}.${extension}`;

if (isKeystoneCloudAsset(mode)) {
if (mode === 'keystone-cloud') {
return await buildKeystoneCloudImageSrc({
apiKey,
imagesDomain,
Expand All @@ -82,7 +82,7 @@ export function createImagesContext(config: KeystoneConfig): ImagesContext | und

const { mode } = imageRef;

if (isKeystoneCloudAsset(mode)) {
if (mode === 'keystone-cloud') {
const { id, extension } = imageRef;
const filename = `${id}.${extension}`;
const metadata = await getImageMetadataFromKeystoneCloud({
Expand All @@ -105,7 +105,7 @@ export function createImagesContext(config: KeystoneConfig): ImagesContext | und
const { upload: mode } = images;
const id = uuid();

if (isKeystoneCloudAsset(mode)) {
if (mode === 'keystone-cloud') {
const metadata = await uploadImageToKeystoneCloud({
apiKey,
stream,
Expand Down
44 changes: 4 additions & 40 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
import { AssetMode, ImageExtension } from '@keystone-next/types';
throw new Error(
'`@keystone-next/utils` has been moved to `@keystone-next/keystone/fields/types/image/utils` for image ref related utilities and `@keystone-next/keystone/fields/types/file/utils` for file ref related utilities, please import from there instead.'
);

const IMAGEREGEX = /^(local|keystone-cloud):image:([^\\\/:\n]+)\.(gif|jpg|png|webp)$/;
const FILEREGEX = /^(local|keystone-cloud):file:([^\\\/:\n]+)/;

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

export const getFileRef = (mode: AssetMode, name: string) => `${mode}:file:${name}`;
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;
};

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

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;
};

export const isLocalAsset = (mode: AssetMode) => mode === 'local';

export const isKeystoneCloudAsset = (mode: AssetMode) => mode === 'keystone-cloud';
export {};
1 change: 0 additions & 1 deletion tests/admin-ui-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
},
"dependencies": {
"@keystone-next/types": "^24.0.0",
"@keystone-next/utils": "^1.0.4",
"@manypkg/find-root": "^1.1.0",
"dotenv": "^10.0.0",
"tree-kill": "^1.2.2"
Expand Down
1 change: 0 additions & 1 deletion tests/api-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
},
"dependencies": {
"@keystone-next/types": "^24.0.0",
"@keystone-next/utils": "^1.0.4",
"apollo-cache-control": "^0.14.0",
"express": "^4.17.1"
}
Expand Down