Skip to content

Commit

Permalink
Merge pull request #7 from omar-dulaimi:typing
Browse files Browse the repository at this point in the history
Typescript Support
  • Loading branch information
omar-dulaimi authored Dec 30, 2022
2 parents 557d3aa + b9bc149 commit d8bd1f3
Show file tree
Hide file tree
Showing 79 changed files with 1,126 additions and 1,200 deletions.
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,27 @@ yarn add trpc-shield
- Don't forget to star this repo 😉

```ts
import * as trpc from '@trpc/server'
import { rule, shield, and, or, not } from 'trpc-shield'
import * as trpc from '@trpc/server';
import { rule, shield, and, or, not } from 'trpc-shield';
import { Context } from '../../src/context';

// Rules

const isAuthenticated = rule()(async (ctx, type, path, input, rawInput) => {
const isAuthenticated = rule<Context>()(async (ctx, type, path, input, rawInput) => {
return ctx.user !== null
})

const isAdmin = rule()(async (ctx, type, path, input, rawInput) => {
const isAdmin = rule<Context>()(async (ctx, type, path, input, rawInput) => {
return ctx.user.role === 'admin'
})

const isEditor = rule()(async (ctx, type, path, input, rawInput) => {
const isEditor = rule<Context>()(async (ctx, type, path, input, rawInput) => {
return ctx.user.role === 'editor'
})

// Permissions

const permissions = shield({
const permissions = shield<Context>({
query: {
frontPage: not(isAuthenticated),
fruits: and(isAuthenticated, or(isAdmin, isEditor)),
Expand Down Expand Up @@ -141,10 +142,10 @@ All rules must be created using the `rule` function.

```jsx
// Normal
const admin = rule()(async (ctx, type, path, input, rawInput) => true)
const admin = rule<Context>()(async (ctx, type, path, input, rawInput) => true)

// With external data
const admin = (bool) => rule(`name-${bool}`)(async (ctx, type, path, input, rawInput) => bool)
const admin = (bool) => rule<Context>(`name-${bool}`)(async (ctx, type, path, input, rawInput) => bool)
```

#### `options`
Expand Down Expand Up @@ -197,19 +198,19 @@ By default `shield` ensures no internal data is exposed to client if it was not
```tsx
import { shield, rule, and, or } from 'trpc-shield'

const isAdmin = rule()(async (ctx, type, path, input, rawInput) => {
const isAdmin = rule<Context>()(async (ctx, type, path, input, rawInput) => {
return ctx.user.role === 'admin'
})

const isEditor = rule()(async (ctx, type, path, input, rawInput) => {
const isEditor = rule<Context>()(async (ctx, type, path, input, rawInput) => {
return ctx.user.role === 'editor'
})

const isOwner = rule()(async (ctx, type, path, input, rawInput) => {
const isOwner = rule<Context>()(async (ctx, type, path, input, rawInput) => {
return ctx.user.items.some((id) => id === parent.id)
})

const permissions = shield({
const permissions = shield<Context>({
query: {
users: or(isAdmin, isEditor),
},
Expand All @@ -224,7 +225,7 @@ const permissions = shield({
tRPC Shield allows you to set a globally defined fallback error that is used instead of `Not Authorised!` default response. This might be particularly useful for localization. You can use `string` or even custom `Error` to define it.

```ts
const permissions = shield(
const permissions = shield<Context>(
{
query: {
items: allow,
Expand All @@ -235,7 +236,7 @@ const permissions = shield(
},
)

const permissions = shield(
const permissions = shield<Context>(
{
query: {
items: allow,
Expand All @@ -252,7 +253,7 @@ const permissions = shield(
Shield allows you to lock-in access. This way, you can seamlessly develop and publish your work without worrying about exposing your data. To lock in your service simply set `fallbackRule` to `deny` like this;

```ts
const permissions = shield(
const permissions = shield<Context>(
{
query: {
users: allow,
Expand Down
134 changes: 67 additions & 67 deletions example/package-lock.json

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

7 changes: 3 additions & 4 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
{
"name": "@examples/express-trpc-shield",
"version": "9.23.4",
"private": true,
"scripts": {
"dev": "ts-node-dev ./src/server"
},
"dependencies": {
"@prisma/client": "^4.8.0",
"@trpc/server": "^10.6.0",
"@trpc/server": "^10.7.0",
"express": "^4.18.2",
"prisma-trpc-generator": "^0.7.2",
"prisma-trpc-generator": "^0.8.2",
"prisma-trpc-shield-generator": "0.0.0-rc.4",
"trpc-shield": "^0.2.0",
"trpc-shield": "^0.2.1",
"typescript": "4.9.4"
},
"devDependencies": {
Expand Down
Binary file modified example/prisma/db.sqlite
Binary file not shown.
11 changes: 8 additions & 3 deletions example/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ generator client {
}

generator trpc {
provider = "prisma-trpc-generator"
output = "./trpc"
provider = "prisma-trpc-generator"
output = "./trpc"
isGenerateSelect = true
isGenerateInclude = true
withMiddleware = true
withShield = true
contextPath = "../../../../src/context"
}

generator trpc_shield {
provider = "prisma-trpc-shield-generator"
output = "./shield"
output = "./shield"
}

datasource db {
Expand Down
13 changes: 9 additions & 4 deletions example/prisma/shield/shield.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { allow, deny, shield } from 'trpc-shield';
import { allow, rule, shield } from '../../../dist'
import { Context } from '../../src/context'

export const permissions = shield({
const isAuthenticated = rule<Context>()(async (ctx, type, path, input, rawInput) => {
return ctx.user !== null
})

export const permissions = shield<Context>({
query: {
aggregateUser: allow,
findFirstUser: allow,
findManyUser: allow,
findManyUser: isAuthenticated,
findUniqueUser: allow,
groupByUser: allow,
},
mutation: {
createOneUser: deny,
createOneUser: allow,
deleteManyUser: allow,
deleteOneUser: allow,
updateManyUser: allow,
Expand Down
Loading

0 comments on commit d8bd1f3

Please sign in to comment.