From 1746698df03e414fa07fe37b1537d873e16d6331 Mon Sep 17 00:00:00 2001 From: Espen Hovlandsdal Date: Tue, 31 Jan 2023 07:42:31 -0800 Subject: [PATCH] refactor: use named export for schema package --- packages/@sanity/block-tools/README.md | 2 +- .../block-tools/test/fixtures/customSchema.ts | 2 +- .../test/fixtures/defaultSchema.ts | 2 +- .../__tests__/PortableTextEditorTester.tsx | 2 +- .../portable-text-editor/src/utils/schema.ts | 2 +- packages/@sanity/schema/example/test.js | 2 +- packages/@sanity/schema/src/_exports/index.ts | 5 ++++- packages/@sanity/schema/src/legacy/Schema.ts | 22 ++++++++++++++++++- .../schema/test/legacy/schemas.test.ts | 2 +- .../@sanity/validation/test/createSchema.ts | 2 +- .../@sanity/validation/test/infer.test.ts | 2 +- .../graphql/extractFromSanitySchema.ts | 2 +- .../core/form/__workshop__/_common/data.ts | 2 +- .../__tests__/ReferenceInput.test.tsx | 2 +- .../form/store/__tests__/collapsible.test.ts | 2 +- .../form/store/__tests__/equality.test.ts | 2 +- .../store/__tests__/members.hidden.test.ts | 2 +- .../sanity/src/core/schema/createSchema.ts | 2 +- .../weighted/createWeightedSearch.test.ts | 2 +- .../search/datastores/recentSearches.test.ts | 2 +- .../utils/createFieldDefinitions.test.tsx | 2 +- .../navbar/search/utils/filterUtils.test.ts | 2 +- .../src/core/templates/__tests__/schema.ts | 2 +- .../documentList/__tests__/helpers.test.ts | 2 +- .../test/cli/graphql/fixtures/test-studio.ts | 2 +- 25 files changed, 48 insertions(+), 25 deletions(-) diff --git a/packages/@sanity/block-tools/README.md b/packages/@sanity/block-tools/README.md index 668d02723fa..d2cc8591518 100644 --- a/packages/@sanity/block-tools/README.md +++ b/packages/@sanity/block-tools/README.md @@ -13,7 +13,7 @@ sanity exec path/to/script.js Let's start with a complete example: ```js -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import {htmlToBlocks} from '@sanity/block-tools' // Start with compiling a schema we can work against diff --git a/packages/@sanity/block-tools/test/fixtures/customSchema.ts b/packages/@sanity/block-tools/test/fixtures/customSchema.ts index bf56d9f1b35..70122cf9f73 100644 --- a/packages/@sanity/block-tools/test/fixtures/customSchema.ts +++ b/packages/@sanity/block-tools/test/fixtures/customSchema.ts @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' export default Schema.compile({ name: 'withCustomBlockType', diff --git a/packages/@sanity/block-tools/test/fixtures/defaultSchema.ts b/packages/@sanity/block-tools/test/fixtures/defaultSchema.ts index f5b98269f6d..c524356716e 100644 --- a/packages/@sanity/block-tools/test/fixtures/defaultSchema.ts +++ b/packages/@sanity/block-tools/test/fixtures/defaultSchema.ts @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' export default Schema.compile({ name: 'withDefaultBlockType', diff --git a/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditorTester.tsx b/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditorTester.tsx index f7d2333831e..94bf98bbd41 100644 --- a/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditorTester.tsx +++ b/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditorTester.tsx @@ -1,5 +1,5 @@ import React, {ForwardedRef, forwardRef, useCallback, useEffect} from 'react' -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import {defineArrayMember, defineField} from '@sanity/types' import {PortableTextEditor, PortableTextEditable} from '../../index' diff --git a/packages/@sanity/portable-text-editor/src/utils/schema.ts b/packages/@sanity/portable-text-editor/src/utils/schema.ts index 73d28b47ef5..b52b887fe51 100644 --- a/packages/@sanity/portable-text-editor/src/utils/schema.ts +++ b/packages/@sanity/portable-text-editor/src/utils/schema.ts @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' export function compileType(rawType: any) { return Schema.compile({ diff --git a/packages/@sanity/schema/example/test.js b/packages/@sanity/schema/example/test.js index 8d3b86ebfee..40669d4ac33 100644 --- a/packages/@sanity/schema/example/test.js +++ b/packages/@sanity/schema/example/test.js @@ -1,5 +1,5 @@ import assert from 'assert' -import Schema from '../src/legacy/Schema' +import {Schema} from '../src/legacy/Schema' import schemaDef from './schema-def' const schema = new Schema(schemaDef) diff --git a/packages/@sanity/schema/src/_exports/index.ts b/packages/@sanity/schema/src/_exports/index.ts index 03ac8915080..b567c432c50 100644 --- a/packages/@sanity/schema/src/_exports/index.ts +++ b/packages/@sanity/schema/src/_exports/index.ts @@ -1,2 +1,5 @@ -export {default} from '../legacy/Schema' +import {Schema as NamedSchema, DeprecatedDefaultSchema} from '../legacy/Schema' + +export default DeprecatedDefaultSchema +export const Schema = NamedSchema export {type SchemaValidationResult} from '../sanity/typedefs' diff --git a/packages/@sanity/schema/src/legacy/Schema.ts b/packages/@sanity/schema/src/legacy/Schema.ts index c449c224895..c10607782b1 100644 --- a/packages/@sanity/schema/src/legacy/Schema.ts +++ b/packages/@sanity/schema/src/legacy/Schema.ts @@ -41,7 +41,7 @@ function compileRegistry(schemaDef) { /** * @beta */ -export default class Schema { +export class Schema { _original: {name: string; types: any[]} _registry: {[typeName: string]: any} @@ -70,3 +70,23 @@ export default class Schema { return Object.keys(this._registry) } } + +/** + * @deprecated Use `import {Schema} from "@sanity/schema"` instead + */ +export class DeprecatedDefaultSchema extends Schema { + static compile(schemaDef: any): Schema { + return new DeprecatedDefaultSchema(schemaDef) + } + + constructor(schemaDef: any) { + super(schemaDef) + + const stack = new Error( + 'The default export of `@sanity/schema` is deprecated. Use `import {Schema} from "@sanity/schema"` instead.' + ).stack.replace(/^Error/, 'Warning') + + // eslint-disable-next-line no-console + console.warn(stack) + } +} diff --git a/packages/@sanity/schema/test/legacy/schemas.test.ts b/packages/@sanity/schema/test/legacy/schemas.test.ts index f26ee9f7cdf..63e2ac34c07 100644 --- a/packages/@sanity/schema/test/legacy/schemas.test.ts +++ b/packages/@sanity/schema/test/legacy/schemas.test.ts @@ -1,4 +1,4 @@ -import Schema from '../../src/legacy/Schema' +import {Schema} from '../../src/legacy/Schema' import rawSchemas from './fixtures/schemas' diff --git a/packages/@sanity/validation/test/createSchema.ts b/packages/@sanity/validation/test/createSchema.ts index c553946dcf3..fa7469ac263 100644 --- a/packages/@sanity/validation/test/createSchema.ts +++ b/packages/@sanity/validation/test/createSchema.ts @@ -1,4 +1,4 @@ -import SchemaBuilder from '@sanity/schema' +import {Schema as SchemaBuilder} from '@sanity/schema' import {validateSchema, groupProblems} from '@sanity/schema/_internal' import {Schema} from '@sanity/types' diff --git a/packages/@sanity/validation/test/infer.test.ts b/packages/@sanity/validation/test/infer.test.ts index 8ba625cd997..0712d61c3f7 100644 --- a/packages/@sanity/validation/test/infer.test.ts +++ b/packages/@sanity/validation/test/infer.test.ts @@ -1,5 +1,5 @@ import {SanityClient} from '@sanity/client' -import SchemaBuilder from '@sanity/schema' +import {Schema as SchemaBuilder} from '@sanity/schema' import {ObjectSchemaType, Rule, SanityDocument} from '@sanity/types' import inferFromSchema from '../src/inferFromSchema' import validateDocument from '../src/validateDocument' diff --git a/packages/sanity/src/_internal/cli/actions/graphql/extractFromSanitySchema.ts b/packages/sanity/src/_internal/cli/actions/graphql/extractFromSanitySchema.ts index b7e22316c35..ddebf62da89 100644 --- a/packages/sanity/src/_internal/cli/actions/graphql/extractFromSanitySchema.ts +++ b/packages/sanity/src/_internal/cli/actions/graphql/extractFromSanitySchema.ts @@ -10,7 +10,7 @@ import type { IntrinsicTypeName, } from '@sanity/types' import {generateHelpUrl} from '@sanity/generate-help-url' -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import oneline from 'oneline' import * as helpUrls from './helpUrls' import {SchemaError} from './SchemaError' diff --git a/packages/sanity/src/core/form/__workshop__/_common/data.ts b/packages/sanity/src/core/form/__workshop__/_common/data.ts index d85d9b0127a..b5120f28070 100644 --- a/packages/sanity/src/core/form/__workshop__/_common/data.ts +++ b/packages/sanity/src/core/form/__workshop__/_common/data.ts @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import type {Schema as SchemaSchema} from '@sanity/types' import {keyBy, mapValues} from 'lodash' import getSimpleDummySchema from './schema/simpleDummySchema' diff --git a/packages/sanity/src/core/form/inputs/ReferenceInput/__tests__/ReferenceInput.test.tsx b/packages/sanity/src/core/form/inputs/ReferenceInput/__tests__/ReferenceInput.test.tsx index 854aa9d8f33..1a8d1166979 100644 --- a/packages/sanity/src/core/form/inputs/ReferenceInput/__tests__/ReferenceInput.test.tsx +++ b/packages/sanity/src/core/form/inputs/ReferenceInput/__tests__/ReferenceInput.test.tsx @@ -1,6 +1,6 @@ import {render} from '@testing-library/react' import React, {forwardRef, useImperativeHandle} from 'react' -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import {LayerProvider, studioTheme, ThemeProvider, ToastProvider} from '@sanity/ui' import {of} from 'rxjs' import {noop} from 'lodash' diff --git a/packages/sanity/src/core/form/store/__tests__/collapsible.test.ts b/packages/sanity/src/core/form/store/__tests__/collapsible.test.ts index d93881d7706..f48c7723342 100644 --- a/packages/sanity/src/core/form/store/__tests__/collapsible.test.ts +++ b/packages/sanity/src/core/form/store/__tests__/collapsible.test.ts @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import {ObjectSchemaType, Path} from '@sanity/types' import {pathToString} from '../../../field' import {prepareFormState} from '../formState' diff --git a/packages/sanity/src/core/form/store/__tests__/equality.test.ts b/packages/sanity/src/core/form/store/__tests__/equality.test.ts index 1a41f3f8e30..7f7dd8369bf 100644 --- a/packages/sanity/src/core/form/store/__tests__/equality.test.ts +++ b/packages/sanity/src/core/form/store/__tests__/equality.test.ts @@ -1,5 +1,5 @@ import {ConditionalProperty} from '@sanity/types' -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import {prepareFormState} from '../formState' import {DEFAULT_PROPS} from './shared' diff --git a/packages/sanity/src/core/form/store/__tests__/members.hidden.test.ts b/packages/sanity/src/core/form/store/__tests__/members.hidden.test.ts index 3ff1185d465..ce2e28a66b4 100644 --- a/packages/sanity/src/core/form/store/__tests__/members.hidden.test.ts +++ b/packages/sanity/src/core/form/store/__tests__/members.hidden.test.ts @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import {ConditionalProperty, ObjectSchemaType} from '@sanity/types' import {prepareFormState} from '../formState' import {DEFAULT_PROPS} from './shared' diff --git a/packages/sanity/src/core/schema/createSchema.ts b/packages/sanity/src/core/schema/createSchema.ts index bb2a0bca506..86140afb9f6 100644 --- a/packages/sanity/src/core/schema/createSchema.ts +++ b/packages/sanity/src/core/schema/createSchema.ts @@ -1,4 +1,4 @@ -import SchemaBuilder, {type SchemaValidationResult} from '@sanity/schema' +import {Schema as SchemaBuilder, type SchemaValidationResult} from '@sanity/schema' import {validateSchema, groupProblems} from '@sanity/schema/_internal' import {Schema} from '@sanity/types' import {inferFromSchema as inferValidation} from '@sanity/validation' diff --git a/packages/sanity/src/core/search/weighted/createWeightedSearch.test.ts b/packages/sanity/src/core/search/weighted/createWeightedSearch.test.ts index 75ef1f9e1fb..ee2862e0cea 100644 --- a/packages/sanity/src/core/search/weighted/createWeightedSearch.test.ts +++ b/packages/sanity/src/core/search/weighted/createWeightedSearch.test.ts @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import {renderHook} from '@testing-library/react' import {defer, lastValueFrom, of} from 'rxjs' import type {SearchTerms} from '..' diff --git a/packages/sanity/src/core/studio/components/navbar/search/datastores/recentSearches.test.ts b/packages/sanity/src/core/studio/components/navbar/search/datastores/recentSearches.test.ts index a94cf93260b..06c1118fd40 100644 --- a/packages/sanity/src/core/studio/components/navbar/search/datastores/recentSearches.test.ts +++ b/packages/sanity/src/core/studio/components/navbar/search/datastores/recentSearches.test.ts @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import type {CurrentUser, ObjectSchemaType} from '@sanity/types' import type {SearchTerms} from '../../../../../search' import {filterDefinitions} from '../definitions/defaultFilters' diff --git a/packages/sanity/src/core/studio/components/navbar/search/utils/createFieldDefinitions.test.tsx b/packages/sanity/src/core/studio/components/navbar/search/utils/createFieldDefinitions.test.tsx index 00da9cb7147..ef05f8be04a 100644 --- a/packages/sanity/src/core/studio/components/navbar/search/utils/createFieldDefinitions.test.tsx +++ b/packages/sanity/src/core/studio/components/navbar/search/utils/createFieldDefinitions.test.tsx @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import React from 'react' import {filterDefinitions} from '../definitions/defaultFilters' import {createFieldDefinitions, MAX_OBJECT_TRAVERSAL_DEPTH} from './createFieldDefinitions' diff --git a/packages/sanity/src/core/studio/components/navbar/search/utils/filterUtils.test.ts b/packages/sanity/src/core/studio/components/navbar/search/utils/filterUtils.test.ts index aaf937d2554..f11a30096cd 100644 --- a/packages/sanity/src/core/studio/components/navbar/search/utils/filterUtils.test.ts +++ b/packages/sanity/src/core/studio/components/navbar/search/utils/filterUtils.test.ts @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import {SearchableType} from '../../../../../search' import {filterDefinitions} from '../definitions/defaultFilters' import {operatorDefinitions} from '../definitions/operators/defaultOperators' diff --git a/packages/sanity/src/core/templates/__tests__/schema.ts b/packages/sanity/src/core/templates/__tests__/schema.ts index 1a4c76cf5f6..79b0e93d4fa 100644 --- a/packages/sanity/src/core/templates/__tests__/schema.ts +++ b/packages/sanity/src/core/templates/__tests__/schema.ts @@ -1,4 +1,4 @@ -import SchemaBuilder from '@sanity/schema' +import {Schema as SchemaBuilder} from '@sanity/schema' import {Schema} from '@sanity/types' const Icon = () => null diff --git a/packages/sanity/src/desk/panes/documentList/__tests__/helpers.test.ts b/packages/sanity/src/desk/panes/documentList/__tests__/helpers.test.ts index 38cd0e9a74f..395a14b5133 100644 --- a/packages/sanity/src/desk/panes/documentList/__tests__/helpers.test.ts +++ b/packages/sanity/src/desk/panes/documentList/__tests__/helpers.test.ts @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' import {ObjectSchemaType} from '@sanity/types' import {applyOrderingFunctions, fieldExtendsType} from '../helpers' diff --git a/packages/sanity/test/cli/graphql/fixtures/test-studio.ts b/packages/sanity/test/cli/graphql/fixtures/test-studio.ts index c061e81fe94..a994917d109 100644 --- a/packages/sanity/test/cli/graphql/fixtures/test-studio.ts +++ b/packages/sanity/test/cli/graphql/fixtures/test-studio.ts @@ -1,4 +1,4 @@ -import Schema from '@sanity/schema' +import {Schema} from '@sanity/schema' export default Schema.compile({ types: [