Giter Club home page Giter Club logo

enonic-wizardry's Introduction

Enonic Wizardry

npm version

Functional utility library for Enonic XP. This library is intended to house reusable and tested code blocks based on enonic-fp that can be used in every project.

Enonic-fp

Enonic-wizardry is intended to supplement enonic-fp with common patterns.

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.

Building the project

npm run build

Usage

Get content by key service

In this example we have a service that returns an article by the key as json. Or if something goes wrong, we return an Internal Server Error instead.

import {fold} from "fp-ts/lib/IOEither";
import {pipe} from "fp-ts/lib/pipeable";
import {Request, Response} from "enonic-types/controller";
import {errorResponse, ok} from "enonic-fp/controller";
import {Article} from "../../site/content-types/article/article"; // 1
import {getContentByIds} from "enonic-wizardry/content";
import {forceArray} from "enonic-fp/array";

export function get(req: Request): Response { // 2
  const keys: Array<string> = forceArray(req.params.key); // ["key1", "key2", "key3"]

  const program = pipe( // 3
    getContentByIds<Article>(keys), // 4
    fold( // 5
      errorResponse(req), // 7
      ok // 8
    )
  );

  return program(); // 9
}
  1. We import an interface Article { ... } generated by xp-codegen-plugin.
  2. We use the imported Request and Response to control the shape of our controller.
  3. We use the pipe function from fp-ts to pipe the result of one function into the next one.
  4. We can use the getContentByIds function from content that query for the Content<Article> where the id is one of the strings in the keys-Array. The return type here is IOEither<EnonicError, ReadonlyArray<Content<Article>>>
  5. The last thing we usually do in pipe is to unpack the IOEither. This is done with fold(handleError, handleSuccess).
  6. The errorResponse(req: Request) function returns a new function that can be used as a callback by fold. This "new function", takes the EnonicError object as a parameter, and creates a Json Response with the correct status number, based on the errorKey of the EnonicError.
  7. We pass the ok function to fold as the second parameter. The ok creates a Response where the status is 200, and the parameter is the body. In this case the ReadonlyArray<Content<Article>> is assigned to thebody.
  8. We have so far constructed a constant program of type IO<Response>, but we have not yet performed a single side effect. It's time to perform those side effects, so we run the IO by calling it, and a Response is returned which out controller function can return.

API

  • Content

    • getContentByIds
    • createAll
    • createAndPublish
    • deleteAndPublish
    • modifyAndPublish
    • applyChangesToData
    • createMediaFromAttachment
  • Context

    • runAsSuperUser
    • runInDraftContext
  • Menu

    • getSubMenuByKey
  • Validation

    • validate

enonic-wizardry's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

enonic-wizardry's Issues

Make building the cli part of `enonic project deploy`

Make the compilation of the typescript code in cli part of the enonic project deploy process.

Create a gradle task, that triggers rendering of all typescript interfaces.

Also document steps for building and running in README.md.

When ` type="ContentSelector"` and maximum = 1, render type as `string`

If we have the case where input is of type ContentSelector, and occurences/@maximum = 1, then Enonic will not return an Array<string>, but only string.

So given this xml:

<input name="fabTarget" type="ContentSelector">
  <label>FAB button target</label>
  <occurrences minimum="0" maximum="1"/>
</input>

We should get:

export interface DefaultPageConfig {
  /**
   * FAB button target
   */
  fabTarget?: string;
}

(and not fabTarget?: Array<string>;)

On `type="ContentSelector"`, generated typescript type should be `Array<string>`.

https://github.com/ItemConsulting/ofo-employee-registry/blob/bde69b8f3e303366bc565a95dddc78bc4093e1bf/src/main/resources/site/content-types/group/group.xml#L15-L21

When <input type="ContentSelector"> the generated ts-code, should have the type Array<string>

Example:

<content-type>
  <display-name>Group</display-name>
  <super-type>base:structured</super-type>
  <form>
    <input name="members" type="ContentSelector">
      <label>Members</label>
      <occurrences minimum="0" maximum="0"/>
      <config>
        <allowContentType>employee</allowContentType>
      </config>
    </input>
  </form>
</content-type>

should generate:

export interface Group {
  members: Array<string>;
}

Generate Typescript interfaces from xml-files

Create a script that can be run from npm, that parses an excel file, and produces a typescript interface file, that will correspond to the json structure created by the content type.

  1. The name of the interface should be determined by the name of the content type (file name), and should be PascalCase. The file name should be the same as the content type file name, but ending with .ts as its type.
  2. Use occurences/@minimum = 0 to determin if the type is nullable or not (see lastName).
  3. The <item-set> element will create nested structures, and needs to be handled recursivly, since the nesting level is not contrained.
<content-type>
  <display-name>Employee</display-name>
  <super-type>base:structured</super-type>
  <form>
    <input type="TextLine" name="firstName">
      <label>First name</label>
      <occurrences minimum="1" maximum="1"/>
    </input>
    <input type="TextLine" name="lastName">
      <label>Last name</label>
      <occurrences minimum="0" maximum="1"/>
    </input>
    <item-set name="contactInfo">
      <label>Contact Info</label>
      <occurrences minimum="0" maximum="0"/>
      <items>
        <input name="label" type="TextLine">
          <label>Label</label>
          <occurrences minimum="0" maximum="1"/>
        </input>
        <input name="phoneNumber" type="TextLine">
          <label>Phone Number</label>
          <occurrences minimum="0" maximum="1"/>
        </input>
      </items>
    </item-set>
  </form>
</content-type>
export interface Employee {
  firstName: string
  lastName?: string
  contactInfo: Array<{
    label: string
    phoneNumber: string
  }>
}

Fix modified files with lint-staged

It looks like the lint-staged configuration is behaving incorrectly. Make sure it is only fixing modified files.

"lint-staged": {
    "*.ts": [
      "eslint --fix 'src/**/*.ts'",
      "git add"
    ]
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.