Skip to content

Commit

Permalink
Merge pull request #13 from pearj/idm7fixes
Browse files Browse the repository at this point in the history
Fix type generation for IDM 7
  • Loading branch information
pearj committed Jul 26, 2021
2 parents 85e03aa + 8959938 commit 01fc499
Show file tree
Hide file tree
Showing 9 changed files with 4,024 additions and 218 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Release
on:
push:
branches:
- master
- beta
jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 12
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Type Check
run: yarn type-check
- name: Type coverage
run: yarn type-coverage --strict --detail
- name: Lint
run: yarn lint
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: yarn semantic-release
3 changes: 3 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
printWidth: 150
arrowParens: avoid
trailingComma: none
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# ForgeRock IDM TypeScript Types
[![npm version](https://img.shields.io/npm/v/@agiledigital/idm-ts-types.svg?style=flat)](https://www.npmjs.com/package/@agiledigital/idm-ts-types)
[![GitHub License](https://img.shields.io/github/license/agiledigital/idm-ts-types.svg)](https://github.com/agiledigital/idm-ts-types/blob/master/LICENSE)
[![Build Status](https://travis-ci.org/agiledigital/idm-ts-types.svg?branch=master)](https://travis-ci.org/agiledigital/idm-ts-types)
[![Release](https://github.com/agiledigital/idm-ts-types/actions/workflows/release.yml/badge.svg)](https://github.com/agiledigital/idm-ts-types/actions/workflows/release.yml)
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fagiledigital%2Fidm-ts-types%2Fmaster%2Fpackage.json)](https://github.com/plantain-00/type-coverage)
[![Known Vulnerabilities](https://snyk.io/test/github/agiledigital/idm-ts-types/badge.svg?targetFile=package.json)](https://snyk.io/test/github/agiledigital/idm-ts-types?targetFile=package.json)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

* TypeScript Support – Write your IDM JavaScript code in TypeScript which has modern language features while still being compatible with Rhino, the JavaScript engine that IDM uses.
* Type Safety – We've put together some TypeScript types that wrap the IDM API to ensure that all your calls to the API are type safe, plus you get the added benefit of getting type-ahead assistance from your IDE.
* Managed Object and Connector Type Generation – We've built a parser that can generate TypeScript types from your Managed Object and Connector types, this means that you can use your Managed Object or Connector types directly in TypeScript which enables IDE type-ahead assistance and type safety.
Expand Down
5 changes: 5 additions & 0 deletions bin/gen-idm-ts-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ function convertType(props, propName, originalObjectName, tsTypeName, subTypes)
let relTypes = filterResourceCollection(props.resourceCollection)
.map(mo => generateManagedTypeName(mo.path.replace("managed/", "")))
.join(" | ");
if (!relTypes) {
relTypes = `Record<string, ${managedObjectValueType}>`;
const otherTypes = props.resourceCollection.map(obj => obj.path).join(", ");
console.warn(`Unable to find managed object type(s) for ${propName}, specified types are [${otherTypes}], falling back to ${relTypes}`);
}
type = `ReferenceType<${relTypes}>`;
break;
default:
Expand Down
54 changes: 43 additions & 11 deletions lib/idm-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ interface IDMObjectType<T extends string> extends IDMBaseObject {
}

type Fields<T> = Exclude<keyof T, "_tag"> & string;
type ResultType<T extends IDMObjectType<string>, FieldTypes extends keyof T> = Pick<T, FieldTypes> & IDMObjectType<Exclude<T["_tag"], undefined>> & Revision;
type ResultType<T extends IDMObjectType<string>, FieldTypes extends keyof T> = Pick<T, FieldTypes> &
IDMObjectType<Exclude<T["_tag"], undefined>> &
Revision;
type QueryFilterTypesafeParams<T extends IDMObjectType<string>> = { filter: Filter<T> };
type QueryFilterExtended<T extends IDMObjectType<string>> = QueryFilter | (QueryFilterTypesafeParams<T> & QueryOpts);

Expand All @@ -27,13 +29,20 @@ export class IDMObject<T extends IDMObjectType<string>, D extends IDMObjectType<
constructor(readonly type: Exclude<T["_tag"], undefined>) {}

public read<F extends Fields<T>>(id: string, options: { readonly params?: object; readonly fields: [F, ...F[]] }): ResultType<T, F> | null;
public read<F extends Fields<T>>(id: string, options: { readonly params?: object; readonly unCheckedFields: string[] }): T & Revision | null;
public read<F extends Fields<T>>(id: string, options?: { readonly params?: object }): D & Revision | null;
public read<F extends Fields<T>>(id: string, { params, fields, unCheckedFields }: { readonly params?: object; readonly fields?: F[]; readonly unCheckedFields?: string[] } = {}) {
public read<F extends Fields<T>>(id: string, options: { readonly params?: object; readonly unCheckedFields: string[] }): (T & Revision) | null;
public read<F extends Fields<T>>(id: string, options?: { readonly params?: object }): (D & Revision) | null;
public read<F extends Fields<T>>(
id: string,
{ params, fields, unCheckedFields }: { readonly params?: object; readonly fields?: F[]; readonly unCheckedFields?: string[] } = {}
) {
return openidm.read(`${this.type}/${id}`, params, unCheckedFields ? unCheckedFields : fields);
}

public create<F extends Fields<T>>(newResourceId: string | null, content: WithOptionalId<T>, options: { readonly params?: object; readonly fields: F[] }): ResultType<T, F>;
public create<F extends Fields<T>>(
newResourceId: string | null,
content: WithOptionalId<T>,
options: { readonly params?: object; readonly fields: F[] }
): ResultType<T, F>;
public create<F extends Fields<T>>(
newResourceId: string | null,
content: WithOptionalId<T>,
Expand All @@ -48,8 +57,18 @@ export class IDMObject<T extends IDMObjectType<string>, D extends IDMObjectType<
return openidm.create(this.type, newResourceId, content, params, unCheckedFields ? unCheckedFields : fields);
}

public patch<F extends Fields<T>>(id: string, rev: string | null, value: PatchOpts[], options: { readonly params?: object; readonly fields: F[] }): ResultType<T, F>;
public patch<F extends Fields<T>>(id: string, rev: string | null, value: PatchOpts[], options: { readonly params?: object; readonly unCheckedFields: F[] }): T & Revision;
public patch<F extends Fields<T>>(
id: string,
rev: string | null,
value: PatchOpts[],
options: { readonly params?: object; readonly fields: F[] }
): ResultType<T, F>;
public patch<F extends Fields<T>>(
id: string,
rev: string | null,
value: PatchOpts[],
options: { readonly params?: object; readonly unCheckedFields: F[] }
): T & Revision;
public patch<F extends Fields<T>>(id: string, rev: string | null, value: PatchOpts[], options?: { readonly params?: object }): D & Revision;
public patch<F extends Fields<T>>(
id: string,
Expand All @@ -60,7 +79,12 @@ export class IDMObject<T extends IDMObjectType<string>, D extends IDMObjectType<
return openidm.patch(`${this.type}/${id}`, rev, value, params, unCheckedFields ? unCheckedFields : fields);
}

public update<F extends Fields<T>>(id: string, rev: string | null, value: WithOptionalId<T>, options: { readonly params?: object; readonly fields: F[] }): ResultType<T, F>;
public update<F extends Fields<T>>(
id: string,
rev: string | null,
value: WithOptionalId<T>,
options: { readonly params?: object; readonly fields: F[] }
): ResultType<T, F>;
public update<F extends Fields<T>>(
id: string,
rev: string | null,
Expand All @@ -78,7 +102,11 @@ export class IDMObject<T extends IDMObjectType<string>, D extends IDMObjectType<
}

public delete<F extends Fields<T>>(id: string, rev: string | null, options: { readonly params?: object; readonly fields: F[] }): ResultType<T, F>;
public delete<F extends Fields<T>>(id: string, rev: string | null, options: { readonly params?: object; readonly unCheckedFields: F[] }): T & Revision;
public delete<F extends Fields<T>>(
id: string,
rev: string | null,
options: { readonly params?: object; readonly unCheckedFields: F[] }
): T & Revision;
public delete<F extends Fields<T>>(id: string, rev: string | null, options?: { readonly params?: object }): D & Revision;
public delete<F extends Fields<T>>(
id: string,
Expand All @@ -91,7 +119,10 @@ export class IDMObject<T extends IDMObjectType<string>, D extends IDMObjectType<
public query<F extends Fields<T>>(params: QueryFilterExtended<T>, options: { readonly fields: F[] }): QueryResult<ResultType<T, F>>;
public query<F extends Fields<T>>(params: QueryFilterExtended<T>, options: { readonly unCheckedFields: string[] }): QueryResult<T & Revision>;
public query<F extends Fields<T>>(params: QueryFilterExtended<T>): QueryResult<D & Revision>;
public query<F extends Fields<T>>(params: QueryFilterExtended<T>, { fields, unCheckedFields }: { readonly fields?: F[]; readonly unCheckedFields?: string[] } = {}) {
public query<F extends Fields<T>>(
params: QueryFilterExtended<T>,
{ fields, unCheckedFields }: { readonly fields?: F[]; readonly unCheckedFields?: string[] } = {}
) {
return openidm.query(this.type, this.flattenFilter(params), unCheckedFields ? unCheckedFields : fields);
}

Expand Down Expand Up @@ -126,4 +157,5 @@ export class IDMObject<T extends IDMObjectType<string>, D extends IDMObjectType<
}
}

export const idmObject = <T extends IDMObjectType<string>, D extends IDMObjectType<string>>(type: Exclude<T["_tag"], undefined>) => new IDMObject<T, D>(type);
export const idmObject = <T extends IDMObjectType<string>, D extends IDMObjectType<string>>(type: Exclude<T["_tag"], undefined>) =>
new IDMObject<T, D>(type);
2 changes: 1 addition & 1 deletion lib/query-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const prepareValue = (val: unknown): string => {
} else {
return (val as any)?.toString() ?? "''";
}
}
};

export const interpretToFilter = <A>(dsl: Filter<A>): string => {
switch (dsl.kind) {
Expand Down
31 changes: 22 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agiledigital/idm-ts-types",
"version": "1.0.14",
"version": "0.0.0-development",
"description": "ForgeRock IDM TypeScript Types",
"homepage": "https://github.com/agiledigital/idm-ts-types",
"bugs": {
Expand All @@ -13,7 +13,7 @@
"contributors": [
{
"name": "Joel Pearson",
"email": "japearson@agiledigital.com.au"
"email": "joel.pearson@gmail.com"
},
{
"name": "Rory Stokes",
Expand All @@ -23,7 +23,10 @@
"license": "Apache-2.0",
"devDependencies": {
"@types/node": "^12.6.8",
"prettier": "^1.18.2",
"commitizen": "^4.2.4",
"cz-conventional-changelog": "3.3.0",
"prettier": "^2.3.2",
"semantic-release": "^17.4.4",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
"tslint-immutable": "^6.0.1",
Expand All @@ -35,20 +38,30 @@
"type-check": "tsc --noEmit",
"type-check:watch": "yarn type-check --watch",
"build:types": "tsc --emitDeclarationOnly --declaration",
"format": "prettier --write \"lib/**/*.ts\" \"bin/**/*.{js,ts}\"",
"lint": "tslint -p tsconfig.json -c tslint.json"
"format": "prettier --config .prettierrc.yaml --write \"lib/**/*.ts\" \"bin/**/*.{js,ts}\"",
"lint": "tslint -p tsconfig.json -c tslint.json",
"semantic-release": "semantic-release",
"commit": "cz"
},
"dependencies": {
"camelcase": "^5.3.1",
"lodash": "^4.17.15",
"nunjucks": "^3.2.2",
"config": "^3.2.2"
"config": "^3.2.2",
"lodash": "^4.17.21",
"nunjucks": "^3.2.2"
},
"bin": {
"gen-idm-ts-types": "bin/gen-idm-ts-types.js"
},
"typeCoverage": {
"atLeast": 98
},
"types": "./lib/idm-globals.d.ts"
"types": "./lib/idm-globals.d.ts",
"publishConfig": {
"access": "public"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
Loading

0 comments on commit 01fc499

Please sign in to comment.