npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shex-codegen

v0.4.9

Published

A library to generate TypeScript code from Shape Expressions.

Downloads

361

Readme

shex-codegen

A library to generate TypeScript code from Shape Expressions.

Usage | Visitors | Features | Contributing

Usage

Install the package via your preferred node package manager: npm i --save-dev shex-codegen or yarn add -D shex-codegen

Add a config file to your project root directory called shex-codegen.yml with roughly the following structure:

# path to the folder or file with shape expressions
schema: "src"
generates:
  # this will be the path of the generated file. It has to end with .ts
  node_modules/@generated/shex.ts:
    # the visitors to visit the schema with
    - typescript

Then you can use the package in one of your scripts e.g.:

{
  ...
  "develop": "... && yarn shex-codegen watch",
  ...

or

{
  ...
  "build": "... && yarn shex-codegen generate",
  ...

Visitors

TypeScript

An example Shape Expression like:

PREFIX srs: <https://shaperepo.com/schemas/solidProfile#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX schem: <http://schema.org/>
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

srs:SolidProfileShape EXTRA a {
  a [ schem:Person ]
    // rdfs:comment  "Declares the node to be a schema.org Person" ;
  a [ foaf:Person ]
    // rdfs:comment  "Declares the node to be a FOAF Person" ;
  vcard:hasPhoto IRI ?
    // rdfs:comment  "A link to the person's photo" ;
  foaf:name xsd:string ?
    // rdfs:comment  "An alternate way to define a person's name" ;
}

becomes

// node_modules/@generated/shex.ts
import { NamedNode, Literal } from "rdflib";
import { Shape } from "shex-methods";

export type SolidProfileShape = {
  id: string; // the url of a node of this shape
  name?: string; // An alternate way to define a person's name
  hasPhoto?: string; // A link to the person's photo
} & {
  type: (
    | SolidProfileShapeType.SchemPerson
    | SolidProfileShapeType.FoafPerson
  )[]; // Defines the node as a Person
};

export type SolidProfileShapeCreateArgs = {
  id: string | NamedNode; // the url to match or create the node with e.g. 'https://example.com#this'
  name?: string | Literal; // An alternate way to define a person's name.
  hasPhoto?: URL | NamedNode; // A link to the person's photo
} & {
  type: (
    | SolidProfileShapeType.SchemPerson
    | SolidProfileShapeType.FoafPerson
  )[]; // Defines the node as a Person (from foaf)
};

export type SolidProfileShapeUpdateArgs = Partial<SolidProfileShapeCreateArgs>;

export enum SolidProfileShapeType {
  SchemPerson = "http://schema.org/Person",
  FoafPerson = "http://xmlns.com/foaf/0.1/Person",
}

export enum SolidProfileShapeContext {
  "type" = "rdf:type",
  "name" = "foaf:name",
  "hasPhoto" = "vcard:hasPhoto",
}

TypeScript methods

For this visitor you will need to additionaly install shex-methods

With a config like this:

# path to the folder or file with shape expressions
schema: "src"
generates:
  # this will be the path of the generated file. It has to end with .ts
  node_modules/@generated/shex.ts:
    # the visitors to visit the schema with
    - typescript
    - typescript-methods

When using the above mentioned Shape Expression and config, this will be added to the generated code:

export const solidProfile = new Shape<SolidProfileShape>({
  id: "https://shaperepo.com/schemas/solidProfile#SolidProfileShape",
  shape: solidProfileShex,
  context: SolidProfileShapeContext,
  type: SolidProfileShapeType,
});

You can then use the Shape object's methods to read, create, update or delete nodes of this shape. See shex-methods or shex-codegen-demo

Features

Existing capabilities:

  • Configure codegen with config file
  • Generate TypeScript types and enums from shex
  • TypeScript operations generator

Some planned features include:

  • TypeScript publisher/subscriber generator (in development)
  • TypeScript react hooks (in development)

Contributing

Use yarn develop to start the build process in watch mode and run the tests on file changes.

When you find a case in which the generated code is incorrect/corrupt please include a snippet of the used shape expression in your pr/issue.