enonic-types
Advanced tools
Comparing version 7.11.0-RC3 to 7.11.0-RC4
116
CHANGELOG.md
# Changelog | ||
## 7.11.0 | ||
> **Note** Enonic has released official types for XP. This library now works as a proxy for those official types, but continues | ||
to provide types for other libraries that there is no official support for yet. | ||
## Changing *tsconfig.json* | ||
> **Warning** Importing types from libraries are broken if you use the old *tsconfig.json*. | ||
If you continue to use the old *tsconfig.json*, importing functions still work, but importing types are now broken. | ||
So we really recommend that you update your *tsconfig.json* to look like the example in [README.md](README.md). | ||
## Upgrading | ||
There are some changes in how the official types are shaped compared to the previous ones. Here are the main ones: | ||
### 1. The shape of `Content` has changed | ||
With 3 type parameters this is the new shape of `Content<Data, Type, Page>`. | ||
Similar to the last version of *enonic-types*, the second type parameter lets the developer set a string literal | ||
that gives the name of the content type. This allows the developer to use `Content.type` as a discriminated union, and | ||
use a simple if-statement to split unions on `type`. | ||
To give the shape of `Content.x` you have to configure the global `XpXData`. | ||
```typescript | ||
import { get, type Content} from "/lib/xp/content"; | ||
import type { Article, Employee } from "../../content-types"; | ||
export function all(req: XP.Request): XP.Response { | ||
const content = get<Content<Article, "myapp:Article"> | Content<Employee, "myapp:Employee">>({ | ||
id: req.params.id | ||
})!; | ||
// By checking `Content.type`, we can now know the shape of `Content.data` | ||
const title = content.type === "myapp:Article" | ||
? content.data.title | ||
: content.data.fullName; | ||
... | ||
} | ||
// You can configure the shape of XData for all content types in your application once | ||
global { | ||
interface XpXData { | ||
"myapp"?: { | ||
"menu-item"?: { | ||
menuItem: boolean; | ||
menuName?: string; | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
### 2. `get`, `query` (etc.) takes `Content<Data>` as a type parameter (instead of just `Data`) | ||
```typescript | ||
import { get, query, type Content, type ContentsResult } from "/lib/xp/content"; | ||
import type { Article, Employee } from "../../content-types"; | ||
export function all(req: XP.Request): XP.Response { | ||
const article = get<Content<Article>>({ id: req.params.id }); | ||
const res = query<Content<Article, "myapp:Article"> | Content<Employee, "myapp:Employee">>({ | ||
contentTypes: ["myapp:Article", "myapp:Employee"] | ||
}); | ||
... | ||
} | ||
``` | ||
### 3. `getContent` and `getComponent` from *portalLib* can return `null` | ||
Personally I will be using the `!` ([Non-null assertion operator](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator)) | ||
when using these functions in a *part* or *layout*. | ||
```typescript | ||
import { getContent, getComponent, type Content } from "/lib/xp/portal"; // Note that portalLib also exports `Content` | ||
import type { Article, Employee } from "../../content-types"; | ||
import type { ArticleList } from "."; | ||
export function all(): XP.Response { | ||
const content = getContent<Content<Article>>()!; // getContent() can now return `null` | ||
const part = getComponent<ArticleList>()!; // getComponent() can now return `null` | ||
... | ||
} | ||
``` | ||
### 4. `Site` takes `<Config>` as a type parameter (again) | ||
You have to pass in the shape of `<Config>` when using `getSiteConfig` or `getSite` from *portalLib*. | ||
```typescript | ||
import { getSiteConfig } from "/lib/xp/portal"; | ||
export function all(): XP.Response { | ||
// If you use "xp-codegen-plugin", you can still use XP.SiteConfig | ||
const siteConfig = getSiteConfig<XP.SiteConfig>()!; | ||
... | ||
} | ||
``` | ||
### 5. There are some types that has changed names | ||
Many types have the same names in the official types – as they had in this library. But some have gotten new names to | ||
conform better with the official naming in XP. | ||
One example of a type that has changed name is contentLibs `QueryResponse` has now become `ContentsResult`. | ||
There are too many other examples to be able to create an extensive list, so you developers just have to look up the new | ||
names as you need them, | ||
## 0.5.0 | ||
@@ -4,0 +120,0 @@ |
@@ -1,2 +0,2 @@ | ||
/// <reference path="node_modules/@item-enonic-types/global/index.d.ts" /> | ||
/// <reference path="../@item-enonic-types/global/index.d.ts" /> | ||
/// <reference path="modules/admin.d.ts" /> | ||
@@ -3,0 +3,0 @@ /// <reference path="modules/auditlog.d.ts" /> |
{ | ||
"name": "enonic-types", | ||
"sideEffects": false, | ||
"version": "7.11.0-RC3", | ||
"version": "7.11.0-RC4", | ||
"description": "TypeScript types for Enonic XP", | ||
@@ -41,28 +41,51 @@ "typings": "index.d.ts", | ||
"dependencies": { | ||
"@enonic-types/core": "^7.11.0-RC3", | ||
"@enonic-types/global": "^7.11.0-RC3", | ||
"@enonic-types/lib-admin": "^7.11.0-RC3", | ||
"@enonic-types/lib-auditlog": "^7.11.0-RC3", | ||
"@enonic-types/lib-auth": "^7.11.0-RC3", | ||
"@enonic-types/lib-cluster": "^7.11.0-RC3", | ||
"@enonic-types/lib-common": "^7.11.0-RC3", | ||
"@enonic-types/lib-content": "^7.11.0-RC3", | ||
"@enonic-types/lib-context": "^7.11.0-RC3", | ||
"@enonic-types/lib-event": "^7.11.0-RC3", | ||
"@enonic-types/lib-export": "^7.11.0-RC3", | ||
"@enonic-types/lib-grid": "^7.11.0-RC3", | ||
"@enonic-types/lib-i18n": "^7.11.0-RC3", | ||
"@enonic-types/lib-io": "^7.11.0-RC3", | ||
"@enonic-types/lib-mail": "^7.11.0-RC3", | ||
"@enonic-types/lib-node": "^7.11.0-RC3", | ||
"@enonic-types/lib-portal": "^7.11.0-RC3", | ||
"@enonic-types/lib-project": "^7.11.0-RC3", | ||
"@enonic-types/lib-repo": "^7.11.0-RC3", | ||
"@enonic-types/lib-scheduler": "^7.11.0-RC3", | ||
"@enonic-types/lib-task": "^7.11.0-RC3", | ||
"@enonic-types/lib-value": "^7.11.0-RC3", | ||
"@enonic-types/lib-vhost": "^7.11.0-RC3", | ||
"@enonic-types/lib-websocket": "^7.11.0-RC3", | ||
"@enonic-types/core": "^7.11.0-RC4", | ||
"@enonic-types/global": "^7.11.0-RC4", | ||
"@enonic-types/lib-admin": "^7.11.0-RC4", | ||
"@enonic-types/lib-auditlog": "^7.11.0-RC4", | ||
"@enonic-types/lib-auth": "^7.11.0-RC4", | ||
"@enonic-types/lib-cluster": "^7.11.0-RC4", | ||
"@enonic-types/lib-common": "^7.11.0-RC4", | ||
"@enonic-types/lib-content": "^7.11.0-RC4", | ||
"@enonic-types/lib-context": "^7.11.0-RC4", | ||
"@enonic-types/lib-event": "^7.11.0-RC4", | ||
"@enonic-types/lib-export": "^7.11.0-RC4", | ||
"@enonic-types/lib-grid": "^7.11.0-RC4", | ||
"@enonic-types/lib-i18n": "^7.11.0-RC4", | ||
"@enonic-types/lib-io": "^7.11.0-RC4", | ||
"@enonic-types/lib-mail": "^7.11.0-RC4", | ||
"@enonic-types/lib-node": "^7.11.0-RC4", | ||
"@enonic-types/lib-portal": "^7.11.0-RC4", | ||
"@enonic-types/lib-project": "^7.11.0-RC4", | ||
"@enonic-types/lib-repo": "^7.11.0-RC4", | ||
"@enonic-types/lib-scheduler": "^7.11.0-RC4", | ||
"@enonic-types/lib-task": "^7.11.0-RC4", | ||
"@enonic-types/lib-value": "^7.11.0-RC4", | ||
"@enonic-types/lib-vhost": "^7.11.0-RC4", | ||
"@enonic-types/lib-websocket": "^7.11.0-RC4", | ||
"@item-enonic-types/lib-cache": "^2.1.1", | ||
"@item-enonic-types/core": "^7.11.0-RC4", | ||
"@item-enonic-types/lib-cron": "^1.1.1", | ||
"@item-enonic-types/lib-explorer": "^3.20.6", | ||
"@item-enonic-types/lib-freemarker": "^2.0.2", | ||
"@item-enonic-types/global": "^7.11.0-RC4", | ||
"@item-enonic-types/lib-graphql": "^2.0.1", | ||
"@item-enonic-types/lib-graphql-playground": "^0.0.1", | ||
"@item-enonic-types/lib-guillotine": "^5.5.0", | ||
"@item-enonic-types/lib-http-client": "^3.1.0", | ||
"@item-enonic-types/lib-menu": "^4.2.0", | ||
"@item-enonic-types/lib-mustache": "^2.1.0", | ||
"@item-enonic-types/lib-notifications": "^2.0.0", | ||
"@item-enonic-types/lib-qrcode": "^2.0.2", | ||
"@item-enonic-types/lib-react4xp": "^2.0.0", | ||
"@item-enonic-types/lib-recaptcha": "^3.0.0", | ||
"@item-enonic-types/lib-router": "^3.0.0", | ||
"@item-enonic-types/lib-sql": "^1.0.0", | ||
"@item-enonic-types/lib-static": "^1.0.2", | ||
"@item-enonic-types/lib-testing": "^7.11.0-RC1", | ||
"@item-enonic-types/lib-text-encoding": "^2.1.0", | ||
"@item-enonic-types/lib-thymeleaf": "^2.0.1-patch2", | ||
"@item-enonic-types/lib-xslt": "^2.0.1", | ||
"@item-enonic-types/lib-turbo-streams": "^1.0.4" | ||
} | ||
} |
196
README.md
@@ -5,26 +5,57 @@ # TypeScript types for Enonic XP | ||
This library contains TypeScript types for Enonic XP. You can get an introduction to usage in this recording from the | ||
Enonic Meetup 13 Apr 2021: | ||
> **Note** There now exists [official TypeScript-types](https://www.npmjs.com/org/enonic-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. | ||
[![TypeScript in Enonic XP | 13 Apr 2021](https://img.youtube.com/vi/2sf-nsG2qU8/default.jpg)](https://www.youtube.com/watch?v=2sf-nsG2qU8) | ||
## Installing individual packages | ||
## Installing *enonic-types* | ||
You can install individual packages with support for Enonic libraries like this: | ||
Install *enonic-types* from npm by running: | ||
You can find the [complete list of supported packages on npm](https://www.npmjs.com/org/item-enonic-types). | ||
```bash | ||
npm i --save-dev @item-enonic-types/core | ||
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: | ||
```bash | ||
npm i --save-dev enonic-types | ||
``` | ||
Add support to use typed `import` updating your *tsconfig.json* file (or *src/main/resources/tsconfig.server.json* if | ||
you are using the [webpack-starter](https://github.com/enonic/starter-webpack)) with the `"types"` field: | ||
## Update tsconfig.json | ||
We recommend using [webpack-starter](https://github.com/enonic/starter-webpack) as the base of your XP-project. | ||
To add the TypeScript-types you need to update your *tsconfig.json* with the following: | ||
```json | ||
{ | ||
"compilerOptions": { | ||
"types": ["node", "enonic-types"], | ||
"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/*"] | ||
} | ||
}, | ||
@@ -39,4 +70,16 @@ "include": [ | ||
And you're ready to start coding! | ||
Note that individual packages that are not directly under `"/lib/..."` needs to be mapped separately. | ||
An example is Freemarker: | ||
```diff | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
+ "/lib/tineikt/freemarker": ["./node_modules/@item-enonic-types/lib-freemarker"] | ||
} | ||
} | ||
} | ||
``` | ||
## Code generation | ||
@@ -51,7 +94,7 @@ | ||
```typescript | ||
import { Article } from "../../site/content-types"; // 1 | ||
import * as contentLib from "/lib/xp/content"; // 2 | ||
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 = contentLib.get<Article>({ | ||
const content = getOne<Content<Article>>({ | ||
key: req.params.id! | ||
@@ -76,111 +119,28 @@ }); | ||
1. We import an `interface Article { ... }` generated by [xp-codegen-plugin](https://github.com/ItemConsulting/xp-codegen-plugin). | ||
2. If you added *enonic-types* to the types in your *tsconfig.json* or *tsconfig.server.json* (see above), TypeScript should now | ||
be able to add types to all the standard XP-libraries. | ||
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`, so we have to do a null check before proceeding. | ||
# Using vanilla JavaScript | ||
If your project is using vanilla JavaScript, you can still get the benefit of code completion when using libraries. | ||
This is even better if you are using [JsDoc to add types to your code](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html). | ||
All you have to do is the following: | ||
1. Configure *./tsconfig.json* as described above | ||
2. Add a file: *./src/main/resources/types.ts* with the following content: | ||
```typescript | ||
declare const require: <K extends keyof XpLibraries | string = string>( | ||
path: K | ||
) => K extends keyof XpLibraries ? XpLibraries[K] : unknown; | ||
declare const resolve: ( | ||
path: string | ||
) => import('enonic-types/thymeleaf').ResourceKey; | ||
declare const app: { | ||
name: string; | ||
version: string; | ||
config: { [key: string]: string }; | ||
}; | ||
declare const log: { | ||
info: (...args: unknown[]) => void; | ||
warning: (...args: unknown[]) => void; | ||
error: (...args: unknown[]) => void; | ||
}; | ||
declare const __: { | ||
newBean: (bean: string) => unknown; | ||
toNativeObject: <A = unknown>(beanResult: A) => A; | ||
}; | ||
``` | ||
## Using `__non_webpack_require__` | ||
If your project is using `__non_webpack_require__`, you should update your *types.ts* file to add type support to it. | ||
If you add (or replace the existing) | ||
`__non_webpack_require__()` function with the following code, it will automatically look up the correct interfaces for | ||
Enonic XP-libraries. | ||
```typescript | ||
declare const __non_webpack_require__: <K extends keyof XpLibraries | string = string>(path: K) => K extends keyof XpLibraries | ||
? XpLibraries[K] | ||
: any; | ||
``` | ||
## Supported libraries | ||
* [AdminLibrary](./src/admin.ts) | ||
* [AuditLogLibrary](src/auditlog.ts) | ||
* [AuthLibrary](./src/auth.ts) | ||
* [CacheLibrary](./src/cache.ts) | ||
* [ClusterLibrary](./src/cluster.ts) | ||
* [CommonLibrary](./src/common.ts) | ||
* [ContentLibrary](./src/content.ts) | ||
* [ContextLibrary](./src/context.ts) | ||
* [ControllerLibrary](./src/controller.ts) | ||
* [CristinLibrary](./src/cristin/index.ts) | ||
* [CronLibrary](./src/cron.ts) | ||
* [EncodingLibrary](./src/encoding.ts) | ||
* [EventLibrary](./src/event.ts) | ||
* [ExplorerLibrary](./src/explorer.ts) | ||
* [ExportLibrary](./src/export.ts) | ||
* [FreeMarkerLibrary](./src/freemarker.ts) | ||
* [GraphQLLibrary](./src/graphql.ts) | ||
* [GridLibrary](./src/grid.ts) | ||
* [GuillotineLibrary](./src/guillotine.ts) | ||
* [HttpLibrary](./src/http.ts) | ||
* [I18nLibrary](./src/i18n.ts) | ||
* [IOLibrary](./src/io.ts) | ||
* [MailLibrary](./src/mail.ts) | ||
* [MenuLibrary](./src/menu.ts) | ||
* [MustacheLibrary](./src/mustache.ts) | ||
* [NodeLibrary](./src/node.ts) | ||
* [NotificationsLibrary](./src/notifications.ts) | ||
* [PortalLibrary](./src/portal.ts) | ||
* [QRLibrary](./src/qr.ts) | ||
* [ProjectLibrary](./src/project.ts) | ||
* [React4XPLibrary](./src/react4xp.ts) | ||
* [RecaptchaLibrary](./src/recaptcha.ts) | ||
* [RepoLibrary](./src/repo.ts) | ||
* [RouterLibrary](./src/router.ts) | ||
* [SchedulerLibrary](./src/scheduler.ts) | ||
* [SentryLibrary](./src/sentry.ts) | ||
* [SessionLibrary](./src/session.ts) | ||
* [SqlLibrary](./src/sql.ts) | ||
* [TaskLibrary](./src/task.ts) | ||
* [TestingLibrary](./src/testing.ts) | ||
* [ThymeleafLibrary](./src/thymeleaf.ts) | ||
* [TurboLibrary](./src/turbo.ts) | ||
* [ValueLibrary](./src/value.ts) | ||
* [VhostLibrary](./src/vhost.ts) | ||
* [WebsocketLibrary](./src/websocket.ts) | ||
* [XsltLibrary](./src/xslt.ts) | ||
## Deployment | ||
You can run the following command to bump all XP-packages: | ||
```bash | ||
npm pkg set version=7.10.0 --workspace packages/xp | ||
``` | ||
* [CacheLibrary](./packages/cache) | ||
* [CronLibrary](./packages/cron) | ||
* [ExplorerLibrary](./packages/explorer) | ||
* [FreeMarkerLibrary](./packages/freemarker) | ||
* [GraphQLLibrary](./packages/graphql) | ||
* [GraphQLPlaygroundLibrary](./packages/graphql-playground) | ||
* [GuillotineLibrary](./packages/guillotine) | ||
* [HttpClientLibrary](./packages/http-client) | ||
* [MenuLibrary](./packages/menu) | ||
* [MustacheLibrary](./packages/mustache) | ||
* [NotificationsLibrary](./packages/notifications) | ||
* [QRCodeLibrary](./packages/qrcode) | ||
* [React4XPLibrary](./packages/react4xp) | ||
* [RecaptchaLibrary](./packages/recaptcha) | ||
* [RouterLibrary](./packages/router) | ||
* [SqlLibrary](./packages/sql) | ||
* [StaticLibrary](./packages/static) | ||
* [TestingLibrary](./packages/testing) | ||
* [TextEncodingLibrary](./packages/text-encoding) | ||
* [ThymeleafLibrary](./packages/thymeleaf) | ||
* [XsltLibrary](./packages/xslt) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
26940
48
50
295
142
+ Added@item-enonic-types/core@7.11.2(transitive)
+ Added@item-enonic-types/lib-cache@2.2.0(transitive)
+ Added@item-enonic-types/lib-cron@1.1.1(transitive)
+ Added@item-enonic-types/lib-explorer@3.20.6(transitive)
+ Added@item-enonic-types/lib-freemarker@2.0.2(transitive)
+ Added@item-enonic-types/lib-graphql@2.0.3(transitive)
+ Added@item-enonic-types/lib-graphql-playground@0.0.1(transitive)
+ Added@item-enonic-types/lib-guillotine@5.5.0(transitive)
+ Added@item-enonic-types/lib-http-client@3.2.1(transitive)
+ Added@item-enonic-types/lib-menu@4.2.1(transitive)
+ Added@item-enonic-types/lib-mustache@2.1.0(transitive)
+ Added@item-enonic-types/lib-notifications@2.0.0(transitive)
+ Added@item-enonic-types/lib-qrcode@2.0.2(transitive)
+ Added@item-enonic-types/lib-react4xp@2.0.0(transitive)
+ Added@item-enonic-types/lib-recaptcha@3.0.0(transitive)
+ Added@item-enonic-types/lib-router@3.0.0(transitive)
+ Added@item-enonic-types/lib-sql@1.0.0(transitive)
+ Added@item-enonic-types/lib-static@1.0.2(transitive)
+ Added@item-enonic-types/lib-testing@7.13.0(transitive)
+ Added@item-enonic-types/lib-text-encoding@2.1.0(transitive)
+ Added@item-enonic-types/lib-thymeleaf@2.1.0(transitive)
+ Added@item-enonic-types/lib-xslt@2.1.1(transitive)