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

enonic-types

v7.13.0

Published

TypeScript types for Enonic XP

Downloads

248

Readme

TypeScript types for Enonic XP

npm version

Note There now exists official TypeScript-types from Enonic. This library will now work as a proxy the official types – where they exist. Its new purpose will be to provide types for all the libraries that doesn't have official support yet.

Installing individual packages

You can install individual packages with support for Enonic libraries like this:

You can find the complete list of supported packages on npm.

npm i --save-dev @item-enonic-types/global
npm i --save-dev @item-enonic-types/lib-menu

Installing all packages

It is also possible to install all packages by installing enonic-types like this:

npm i --save-dev enonic-types

Update tsconfig.json

We recommend using webpack-starter as the base of your XP-project.

To add the TypeScript-types you need to update your tsconfig.json with the following:

{
  "compilerOptions": {
    "target": "es5",
    "strict": true,
    "sourceMap": true,
    "allowJs": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "types": ["node", "@item-enonic-types/global"],
    "rootDirs": [
      "./src/main/resources",
      "./.xp-codegen"
    ],
    "paths": {
      "/lib/xp/*": ["./node_modules/@enonic-types/lib-*"],
      "/lib/*": [ "./node_modules/@item-enonic-types/lib-*" ,"./src/main/resources/lib/*"],
      "/site/*": ["./.xp-codegen/site/*"]
    }
  },
  "include": [
    "./.xp-codegen/**/*",
    "./src/main/resources/**/*"
  ],
  "exclude": ["./build/*"]
}

Note that individual packages that are not directly under "/lib/..." needs to be mapped separately.

An example is Freemarker:

{
  "compilerOptions": {
    "paths": {
+     "/lib/tineikt/freemarker": ["./node_modules/@item-enonic-types/lib-freemarker"]
    }
  }
}

Code generation

We recommend using this library together with the xp-codegen-plugin Gradle plugin. xp-codegen-plugin will create TypeScript interfaces for your content-types. Those interfaces will be very useful together with this library.

Example

We have an Enonic service that returns an article by id.

import type { Article } from "../../site/content-types"; // 1
import { get as getOne, type Content } from "/lib/xp/content"; // 2

export function get(req: XP.Request): XP.Response { // 3
  const content = getOne<Content<Article>>({ 
    key: req.params.id!
  });

  assertIsDefined(content); // 4

  const article: Article = content.data;
  
  return {
    status: 200,
    body: article
  }
}

/** 
 * Create this function in a utilities file of your choice... 
 */
function assertIsDefined<T>(value: T): asserts value is NonNullable<T> {
  if (value === undefined || value === null) {
    throw new Error(`${value} is not defined`);
  }
}
  1. We import an interface Article { ... } generated by xp-codegen-plugin.
  2. The standard XP-libraries are mapped to their paths by the change to tsconfig.json.
  3. We use XP.Request and XP.Response to control the shape of our controller.
  4. content is of the type Content<Article> | null, but we can "assert" that it is not nullable (or throw an exception here if it is).

Supported libraries