Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

feat(v1): generate identifiers #299

Open
wants to merge 5 commits into
base: alpha
Choose a base branch
from

Conversation

ochicf
Copy link
Contributor

@ochicf ochicf commented May 26, 2023

Broken generation of identifiers in 2 function that can be provided in config:

  • generateWorkspaceName: allows to generate workspace identifiers
  • generateTypeName: allows to generate type identifiers (for top level schema nodes)

Some notes on the idea behind the implementation:

  • I have separated this into 2 function as I see a benefit of having just one for workspaces
  • both function have a similar signatures:
    • first parameter is the an already typescript ready identifier, which has been generated from the schema/node name using the default generate type function (so it can be directly returned)
    • second parameter is an object that gives more context of the schema/node. The intent is to:
      • give access to the raw schema/node name (to generate it from it, use it in a switch, etc)
      • give access to other schemas/nodes in case custom logic to avoid duplicate names is needed
      • in case of the nodes, give access to the schema being used on in case is needed
    • the returned string should be a valid typescript identifier, and no sanitisation is done

Actual signatures:

function generateTypeName(
  typeName: string,
  context: {
    normalizedSchema: Sanity.SchemaDef.Schema;
    node: Sanity.SchemaDef.DocumentNode | Sanity.SchemaDef.RegisteredSchemaNode;
    nodes: (Sanity.SchemaDef.DocumentNode | Sanity.SchemaDef.RegisteredSchemaNode)[];
  },
): string;

function generateWorkspaceName(
  typeName: string,
  context: {
    normalizedSchemas: Sanity.SchemaDef.Schema[];
    normalizedSchema: Sanity.SchemaDef.Schema;
  },
): string;

Questions:

  • should the defaultGenerateTypeName helper be renamed to sanityNameToTypeName or sanityNameToIdentifier?
  • should specific usage examples be added in the docs?

@vercel
Copy link

vercel bot commented May 26, 2023

@ochicf is attempting to deploy a commit to the Rico Kahler Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant