@nhost/stripe-graphql-js 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +155 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Nhost
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
<h1>⚠️ Work In Progress ⚠️</h1>
|
|
2
|
+
|
|
3
|
+
**This package being actively worked on and is NOT stable!**
|
|
4
|
+
|
|
5
|
+
<h1 align="center">@nhost/stripe-graphql-js</h1>
|
|
6
|
+
<h2 align="center">Stripe GraphQL API</h2>
|
|
7
|
+
|
|
8
|
+
<p align="center">
|
|
9
|
+
<img alt="npm" src="https://img.shields.io/npm/v/@nhost/stripe-graphql">
|
|
10
|
+
<img alt="npm" src="https://img.shields.io/npm/dm/@nhost/stripe-graphql">
|
|
11
|
+
<a href="LICENSE">
|
|
12
|
+
<img src="https://img.shields.io/badge/license-MIT-yellow.svg" alt="license: MIT" />
|
|
13
|
+
</a>
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
Stripe GraphQL API with Hasura Remote schemas.
|
|
17
|
+
|
|
18
|
+
Connect data in your database with data from Stripe, via GraphQL.
|
|
19
|
+
|
|
20
|
+
```graphql
|
|
21
|
+
query {
|
|
22
|
+
users {
|
|
23
|
+
# User in your database
|
|
24
|
+
id
|
|
25
|
+
displayName
|
|
26
|
+
userData {
|
|
27
|
+
stripeCustomerId # Customer's Stripe Customer Id
|
|
28
|
+
stripeCustomer {
|
|
29
|
+
# Data from Stripe
|
|
30
|
+
id
|
|
31
|
+
name
|
|
32
|
+
paymentMethods {
|
|
33
|
+
id
|
|
34
|
+
card {
|
|
35
|
+
brand
|
|
36
|
+
last4
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Install
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npm install @nhost/stripe-graphql-js
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Quick Start
|
|
52
|
+
|
|
53
|
+
### Serverless Function Setup
|
|
54
|
+
|
|
55
|
+
Create a new [Serverless Function](https://docs.nhost.io/platform/serverless-functions) `functions/graphql/stripe.ts`:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
import { createStripeGraphQLServer } from '@nhost/stripe-graphql-js'
|
|
59
|
+
|
|
60
|
+
const server = createStripeGraphQLServer()
|
|
61
|
+
|
|
62
|
+
export default server
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Test
|
|
66
|
+
|
|
67
|
+
Test the Stripe GraphQL API in the rowser:
|
|
68
|
+
|
|
69
|
+
[http://localhost:1337/v1/functions/graphql/stripe](http://localhost:1337/v1/functions/graphql/stripe)
|
|
70
|
+
|
|
71
|
+
### Remote Schema
|
|
72
|
+
|
|
73
|
+
Add the Stripe GraphQL API as a Remote Schema in Hasura.
|
|
74
|
+
|
|
75
|
+
URL: `{{NHOST_BACKEND_URL}}/v1/functions/graphql/stripe`
|
|
76
|
+
|
|
77
|
+
## Permissions
|
|
78
|
+
|
|
79
|
+
Here's a minimal example without any custom permissions. Only requests using the `x-hasura-admin-secret` header will work:
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
const server = createStripeGraphQLServer()
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
For more granular permissions, you can pass an `isAllowed` function to the `createStripeGraphQLServer`. The `isAllowed` function takes a `stripeCustomerId` and [`context`](#context) as parameters and runs every time the GraphQL server makes a request to Stripe to get or modify data for a specific Stripe customer.
|
|
86
|
+
|
|
87
|
+
Here is an example of an `isAllowed` function:
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
|
|
91
|
+
const isAllowed = (stripeCustomerId: string, context: Context) => {
|
|
92
|
+
const { isAdmin, userClaims } = context
|
|
93
|
+
|
|
94
|
+
// allow requests if it has a valid `x-hasura-admin-secret`
|
|
95
|
+
if (isAdmin) {
|
|
96
|
+
return true
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// get user id
|
|
100
|
+
const userId = userClaims['x-hasura-user-id']
|
|
101
|
+
|
|
102
|
+
// check if user is signed in
|
|
103
|
+
if (!userId) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// get more user information from the database
|
|
108
|
+
const { user } = await gqlSDK.getUser({
|
|
109
|
+
id: userId,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
if (!user) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// check if the user is part of a workspace with the `stripeCustomerId`
|
|
117
|
+
return user.workspaceMembers
|
|
118
|
+
.some((workspaceMember) => {
|
|
119
|
+
return workspaceMember.workspace.stripeCustomerId === stripeCustomerId;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Context
|
|
126
|
+
|
|
127
|
+
The `context` object contains:
|
|
128
|
+
|
|
129
|
+
- `userClaims` - verified JWT claims from the user's access token.
|
|
130
|
+
- `isAdmin` - `true` if the request was made using a valid `x-hasura-admin-secret` header.
|
|
131
|
+
- `request` - [Fetch API Request object](https://developer.mozilla.org/en-US/docs/Web/API/Request) that represents the incoming HTTP request in platform-independent way. It can be useful for accessing headers to authenticate a user
|
|
132
|
+
- `query` - the DocumentNode that was parsed from the GraphQL query string
|
|
133
|
+
- `operationName` - the operation name selected from the incoming query
|
|
134
|
+
- `variables` - the variables that were defined in the query
|
|
135
|
+
- `extensions` - the extensions that were received from the client
|
|
136
|
+
|
|
137
|
+
Read more about the [default context from GraphQL Yoga](https://www.the-guild.dev/graphql/yoga-server/docs/features/context#default-context).
|
|
138
|
+
|
|
139
|
+
## Development
|
|
140
|
+
|
|
141
|
+
Install dependencies:
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
pnpm install
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Start the development server:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
pnpm dev
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Open GraphiQL:
|
|
154
|
+
|
|
155
|
+
[http://0.0.0.0:4000/graphql](http://0.0.0.0:4000/graphql)
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nhost/stripe-graphql-js",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Stripe GraphQL API",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"stripe",
|
|
8
|
+
"graphql",
|
|
9
|
+
"api",
|
|
10
|
+
"nhost",
|
|
11
|
+
"hasura"
|
|
12
|
+
],
|
|
13
|
+
"author": "Nhost",
|
|
14
|
+
"homepage": "https://nhost.io",
|
|
15
|
+
"bugs": "https://github.com/nhost/nhost/issues",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/nhost/nhost.git"
|
|
19
|
+
},
|
|
20
|
+
"main": "dist/index.js",
|
|
21
|
+
"types": "dist/index.d.ts",
|
|
22
|
+
"source": "src/index.ts",
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@graphql-yoga/node": "^2.13.13",
|
|
32
|
+
"@pothos/core": "^3.21.0",
|
|
33
|
+
"graphql": "16.6.0",
|
|
34
|
+
"graphql-scalars": "^1.18.0",
|
|
35
|
+
"jsonwebtoken": "^8.5.1",
|
|
36
|
+
"stripe": "^10.10.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/jsonwebtoken": "^8.5.9",
|
|
40
|
+
"@types/node": "^18.7.14",
|
|
41
|
+
"dotenv": "^16.0.2",
|
|
42
|
+
"ts-node-dev": "^2.0.0",
|
|
43
|
+
"typescript": "^4.8.2"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"dev": "NODE_ENV=development ts-node-dev -r dotenv/config dev/server.ts",
|
|
47
|
+
"build": "tsc",
|
|
48
|
+
"test:watch": "vitest",
|
|
49
|
+
"test:coverage": "vitest run --coverage",
|
|
50
|
+
"prettier": "prettier --check src/",
|
|
51
|
+
"prettier:fix": "prettier --write src/",
|
|
52
|
+
"lint": "eslint . --ext .ts,.tsx",
|
|
53
|
+
"lint:fix": "eslint . --ext .ts,.tsx --fix",
|
|
54
|
+
"verify": "run-p prettier lint",
|
|
55
|
+
"verify:fix": "run-p prettier:fix lint:fix"
|
|
56
|
+
}
|
|
57
|
+
}
|