Skip to content

Commit

Permalink
feat: OPL typescript library on npm
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Sep 22, 2022
1 parent b6c93ba commit 446fe7d
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/npm_publish_grpc_client.yml
Expand Up @@ -28,8 +28,8 @@ jobs:
env:
RELEASE_VERSION: ${{ github.event.inputs.version || github.ref }}
- run: |-
git config --local user.email "zepatrik@users.noreply.github.com"
git config --local user.name "zepatrik"
git config --global user.email "60093411+ory-bot@users.noreply.github.com"
git config --global user.name "ory-bot"
git add proto/package.json
git commit -m "autogen: bump node gRPC client version"
- run: |-
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/npm_publish_typelib.yml
@@ -0,0 +1,39 @@
name: Publish OPL typelib to npm

on:
release:
types:
- created
workflow_dispatch:
inputs:
version:
required: true
description: The version to release

jobs:
btp:
runs-on: ubuntu-latest
name: Publish
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "14"
registry-url: "https://registry.npmjs.org"
- run: sudo npm i -g npm@7
- name: Bump version
run: |-
cd contrib/namespace-type-lib
cat <<< $(jq '.version = (env.RELEASE_VERSION | sub("(^refs/tags/v)|(^v)"; ""))' package.json) > package.json
env:
RELEASE_VERSION: ${{ github.event.inputs.version || github.ref }}
- run: |-
git config --global user.email "60093411+ory-bot@users.noreply.github.com"
git config --global user.name "ory-bot"
git add contrib/namespace-type-lib/package.json
git commit -m "autogen: bump OPL typelib"
- run: |-
cd contrib/namespace-type-lib
npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN_AENEASR }}
34 changes: 34 additions & 0 deletions contrib/namespace-type-lib/index.d.ts
@@ -0,0 +1,34 @@
/// <reference no-default-lib="true"/>

declare interface Boolean {}
declare interface String {}
declare interface Number {}
declare interface Function {}
declare interface Object {}
declare interface IArguments {}
declare interface RegExp {}

declare interface Array<T extends namespace> {
includes(element: T): boolean
traverse(iteratorfn: (element: T) => boolean): boolean
}

interface context {
subject: never
}

interface namespace {
related?: { [relation: string]: namespace[] }
permits?: { [method: string]: (ctx: context) => boolean }
}

declare module "@ory/keto-namespace-types" {
export type Context = context

export type Namespace = namespace

export type SubjectSet<
A extends Namespace,
R extends keyof A["related"],
> = A["related"][R] extends Array<infer T> ? T : never
}
58 changes: 58 additions & 0 deletions contrib/namespace-type-lib/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions contrib/namespace-type-lib/package.json
@@ -0,0 +1,21 @@
{
"name": "@ory/keto-namespace-types",
"version": "0.9.0-alpha.0",
"description": "TypeScript definitions for Ory Keto Namespaces",
"homepage": "https://www.ory.sh/keto",
"bugs": "https://github.com/ory/keto/issues",
"main": "",
"types": "index.d.ts",
"files": [
"index.d.ts",
"tsconfig.json"
],
"scripts": {
"test": "tsc --types --noEmit --noLib test.ts"
},
"dependencies": {},
"devDependencies": {
"@ory/keto-namespace-types": "file:./",
"typescript": "^4.7.4"
}
}
36 changes: 36 additions & 0 deletions contrib/namespace-type-lib/test.ts
@@ -0,0 +1,36 @@
import { Namespace, SubjectSet, Context } from "@ory/keto-namespace-types"

// This test is not really a valid config, but rather a check of the types.
class User implements Namespace {
related: {
friends: User[]
}
}

class Group implements Namespace {
related: {
members: (User | Group)[]
}

permits = {
isMember: (ctx: Context): boolean =>
this.related.members.traverse((m) =>
m instanceof User ? m == ctx.subject : m.permits.isMember(ctx),
),
}
}

class File implements Namespace {
related: {
viewers: (User | SubjectSet<Group, "members">)[]
}

permits = {
view: (ctx: Context): boolean =>
this.related.viewers.traverse((p) =>
p instanceof User
? p.related.friends.includes(ctx.subject)
: p.permits.isMember(ctx),
) || this.related.viewers.includes(ctx.subject),
}
}
7 changes: 7 additions & 0 deletions contrib/namespace-type-lib/tsconfig.json
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"noLib": true,
"noEmit": true,
"types": []
}
}
18 changes: 0 additions & 18 deletions contrib/rewrites-example/lib.ts

This file was deleted.

2 changes: 1 addition & 1 deletion contrib/rewrites-example/namespaces.keto.ts
@@ -1,4 +1,4 @@
/// <reference path="./lib.ts" />
import { Namespace, SubjectSet, Context } from "@ory/keto-namespace-types"

class User implements Namespace {
related: {
Expand Down
78 changes: 78 additions & 0 deletions contrib/rewrites-example/namespaces.ts
@@ -0,0 +1,78 @@
import { Namespace, SubjectSet, Context } from "@ory/keto-namespace-types"

// Declare new namespaces as classes that implement `Namespaces`
class User implements Namespace {
related: {
// Define relations to other objects here.
// Examples:
//
// parents: (File | Folder)[]
// viewers: SubjectSet<Group, "members">[]
}

permits = {
// Define permissions here. These can be derived from the relations above.
// Examples:
//
// view: (ctx: Context): boolean =>
// this.related.viewers.includes(ctx.subject) ||
// this.related.parents.traverse((p) => p.permits.view(ctx)),
}
}

class Group implements Namespace {
related: {
// Define relations to other objects here.
// Examples:
//
// parents: (File | Folder)[]
// viewers: SubjectSet<Group, "members">[]
}

permits = {
// Define permissions here. These can be derived from the relations above.
// Examples:
//
// view: (ctx: Context): boolean =>
// this.related.viewers.includes(ctx.subject) ||
// this.related.parents.traverse((p) => p.permits.view(ctx)),
}
}

class Folder implements Namespace {
related: {
// Define relations to other objects here.
// Examples:
//
// parents: (File | Folder)[]
// viewers: SubjectSet<Group, "members">[]
}

permits = {
// Define permissions here. These can be derived from the relations above.
// Examples:
//
// view: (ctx: Context): boolean =>
// this.related.viewers.includes(ctx.subject) ||
// this.related.parents.traverse((p) => p.permits.view(ctx)),
}
}

class File implements Namespace {
related: {
// Define relations to other objects here.
// Examples:
//
// parents: (File | Folder)[]
// viewers: SubjectSet<Group, "members">[]
}

permits = {
// Define permissions here. These can be derived from the relations above.
// Examples:
//
// view: (ctx: Context): boolean =>
// this.related.viewers.includes(ctx.subject) ||
// this.related.parents.traverse((p) => p.permits.view(ctx)),
}
}
56 changes: 56 additions & 0 deletions contrib/rewrites-example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contrib/rewrites-example/package.json
@@ -0,0 +1,7 @@
{
"private": true,
"devDependencies": {
"@ory/keto-namespace-types": "file:../namespace-type-lib",
"typescript": "latest"
}
}
7 changes: 7 additions & 0 deletions contrib/rewrites-example/tsconfig.json
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"noLib": true,
"noEmit": true,
"types": []
}
}

0 comments on commit 446fe7d

Please sign in to comment.