@nhost/stripe-graphql-js 0.0.2 → 0.0.4

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/README.md CHANGED
@@ -1,21 +1,36 @@
1
- <h1>⚠️ Work In Progress ⚠️</h1>
2
-
3
- **This package being actively worked on and is NOT stable!**
4
-
5
1
  <h1 align="center">@nhost/stripe-graphql-js</h1>
6
2
  <h2 align="center">Stripe GraphQL API</h2>
7
3
 
8
4
  <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">
5
+ <img alt="npm" src="https://img.shields.io/npm/v/@nhost/stripe-graphql-js">
6
+ <img alt="npm" src="https://img.shields.io/npm/dm/@nhost/stripe-graphql-js">
11
7
  <a href="LICENSE">
12
8
  <img src="https://img.shields.io/badge/license-MIT-yellow.svg" alt="license: MIT" />
13
9
  </a>
14
10
  </p>
15
11
 
16
- Stripe GraphQL API with Hasura Remote schemas.
12
+ This package creates a Stripe GraphQL API.
17
13
 
18
- Connect data in your database with data from Stripe, via GraphQL.
14
+ ```graphql
15
+ query {
16
+ stripe {
17
+ customer(id: "cus_xxx" {
18
+ id
19
+ name
20
+ invoices {
21
+ data {
22
+ id
23
+ created
24
+ paid
25
+ hostedInvoiceUrl
26
+ }
27
+ }
28
+ }
29
+ }
30
+ }
31
+ ```
32
+
33
+ You can also add the Stripe GraphQL API as a Hasura Remote Schema and connect data from your database and Stripe. This allows you to request data from your database and Stripe in a single GraphQL query:
19
34
 
20
35
  ```graphql
21
36
  query {
@@ -62,9 +77,29 @@ const server = createStripeGraphQLServer()
62
77
  export default server
63
78
  ```
64
79
 
80
+ > You can run the Stripe GraphQL API in any JS environment because it's built using [GraphQL Yoga](https://github.com/dotansimha/graphql-yoga).
81
+
82
+ ### Stripe Secret Key
83
+
84
+ Add `STRIPE_SECRET_KEY` as an environment variable. If you're using Nhost, add `STRIPE_SECRET_KEY` to `.env.development` like this:
85
+
86
+ ```
87
+ STRIPE_SECRET_KEY=sk_test_xxx
88
+ ```
89
+
90
+ Learn more about [Stripe API keys](https://stripe.com/docs/keys#obtain-api-keys).
91
+
92
+ ### Start Nhost
93
+
94
+ ```
95
+ nhost up
96
+ ```
97
+
98
+ Learn more about the [Nhost CLI](https://docs.nhost.io/platform/cli).
99
+
65
100
  ### Test
66
101
 
67
- Test the Stripe GraphQL API in the rowser:
102
+ Test the Stripe GraphQL API in the browser:
68
103
 
69
104
  [http://localhost:1337/v1/functions/graphql/stripe](http://localhost:1337/v1/functions/graphql/stripe)
70
105
 
@@ -72,7 +107,19 @@ Test the Stripe GraphQL API in the rowser:
72
107
 
73
108
  Add the Stripe GraphQL API as a Remote Schema in Hasura.
74
109
 
75
- URL: `{{NHOST_BACKEND_URL}}/v1/functions/graphql/stripe`
110
+ **URL**
111
+
112
+ ```
113
+ {{NHOST_BACKEND_URL}}/v1/functions/graphql/stripe
114
+ ```
115
+
116
+ **Headers**
117
+
118
+ ```
119
+ x-nhost-webhook-secret: NHOST_WEBHOOK_SECRET (from env var)
120
+ ```
121
+
122
+ ![Hasura Remote Schema](./assets//hasura-remote-schema.png)
76
123
 
77
124
  ## Permissions
78
125
 
@@ -150,6 +197,8 @@ Start the development server:
150
197
  pnpm dev
151
198
  ```
152
199
 
200
+ The GraphQL Server will reload every time the code changes.
201
+
153
202
  Open GraphiQL:
154
203
 
155
204
  [http://0.0.0.0:4000/graphql](http://0.0.0.0:4000/graphql)
@@ -23,7 +23,7 @@ builder_1.builder.objectType('Stripe', {
23
23
  },
24
24
  resolve: (_parent, { id }, context) => __awaiter(void 0, void 0, void 0, function* () {
25
25
  const { isAllowed } = context;
26
- if (!isAllowed(id, context)) {
26
+ if (!(yield isAllowed(id, context))) {
27
27
  throw new node_1.GraphQLYogaError('Not allowed');
28
28
  }
29
29
  const customer = yield utils_1.stripe.customers.retrieve(id);
@@ -61,7 +61,12 @@ builder_1.builder.objectType('Stripe', {
61
61
  ending_before: endingBefore || undefined,
62
62
  starting_after: startingAfter || undefined
63
63
  });
64
- const customerData = customers.data.filter((customer) => isAllowed(customer.id, context));
64
+ const customerData = [];
65
+ for (const customer of customers.data) {
66
+ if (yield isAllowed(customer.id, context)) {
67
+ customerData.push(customer);
68
+ }
69
+ }
65
70
  customers.data = customerData;
66
71
  return customers;
67
72
  })
@@ -88,7 +93,7 @@ builder_1.builder.objectType('StripeMutations', {
88
93
  },
89
94
  resolve: (_, { customer, configuration, locale, returnUrl }, context) => __awaiter(void 0, void 0, void 0, function* () {
90
95
  const { isAllowed } = context;
91
- if (!isAllowed(customer, context)) {
96
+ if (!(yield isAllowed(customer, context))) {
92
97
  throw new node_1.GraphQLYogaError('Not allowed');
93
98
  }
94
99
  const session = yield utils_1.stripe.billingPortal.sessions.create({
@@ -1 +1 @@
1
- {"version":3,"file":"stripe.js","sourceRoot":"","sources":["../../src/schema/stripe.ts"],"names":[],"mappings":";;;;;;;;;;;AAEA,6CAAqD;AAErD,wCAAoC;AACpC,oCAAiC;AAEjC,iBAAO,CAAC,UAAU,CAAC,QAAQ,EAAE;IAC3B,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACd,QAAQ,EAAE,CAAC,CAAC,KAAK,CAAC;YAChB,IAAI,EAAE,gBAAgB;YACtB,IAAI,EAAE;gBACJ,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC;oBACf,QAAQ,EAAE,IAAI;iBACf,CAAC;aACH;YACD,OAAO,EAAE,CAAO,OAAO,EAAE,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE;gBAC1C,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CAAA;gBAE7B,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,OAAO,CAAC,EAAE;oBAC3B,MAAM,IAAI,uBAAgB,CAAC,aAAa,CAAC,CAAA;iBAC1C;gBAED,MAAM,QAAQ,GAAG,MAAM,cAA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E;oBACvC,MAAM,IAAI,uBAAgB,CAAC,aAAa,CAAC,CAAA;iBAC1C;gBAED,MAAM,OAAO,GAAG,MAAM,cAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,MAAM,CAAC;oBACzD,QAAQ;oBACR,aAAa,EAAE,aAAa,IAAI,SAAS;oBACzC,MAAM,EAAG,MAA0D,IAAI,SAAS;oBAChF,UAAU,EAAE,SAAS,IAAI,SAAS;iBACnC,CAAC,CAAA;gBAEF,OAAO,OAAO,CAAA;YAChB,CAAC,CAAA;SACF,CAAC;KACH,CAAC;CACH,CAAC,CAAA;AAEF,iBAAO,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC1B,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC;QACd,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;KACpB,CAAC;CACH,CAAC,CAAC,CAAA;AAEH,iBAAO,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC7B,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC;QACd,IAAI,EAAE,iBAAiB;QACvB,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;KACpB,CAAC;CACH,CAAC,CAAC,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAGpD,QAAA,MAAM,yBAAyB,YAAa,iBAAiB;;;eA6C5D,CAAA;AAED,OAAO,EAAE,yBAAyB,EAAE,MAAM,EAAE,CAAA"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAGpD,QAAA,MAAM,yBAAyB,YAAa,iBAAiB;;;eA+C5D,CAAA;AAED,OAAO,EAAE,yBAAyB,EAAE,MAAM,EAAE,CAAA"}
package/dist/server.js CHANGED
@@ -8,6 +8,7 @@ const utils_1 = require("./utils");
8
8
  const createStripeGraphQLServer = (params) => {
9
9
  const cors = params === null || params === void 0 ? void 0 : params.cors;
10
10
  const isAllowed = params === null || params === void 0 ? void 0 : params.isAllowed;
11
+ const graphiql = params === null || params === void 0 ? void 0 : params.graphiql;
11
12
  const context = (context) => {
12
13
  const { request } = context;
13
14
  // user id
@@ -33,6 +34,7 @@ const createStripeGraphQLServer = (params) => {
33
34
  };
34
35
  return (0, node_1.createServer)({
35
36
  cors,
37
+ graphiql,
36
38
  context,
37
39
  schema: schema_1.schema
38
40
  });
@@ -1 +1 @@
1
- {"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";;;AAAA,6CAAqE;AAErE,qCAAiC;AAmDG,uFAnD3B,eAAM,OAmD2B;AAjD1C,mCAAuC;AAEvC,MAAM,yBAAyB,GAAG,CAAC,MAA0B,EAAE,EAAE;IAC/D,MAAM,IAAI,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,CAAA;IACzB,MAAM,SAAS,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,SAAS,CAAA;IAEnC,MAAM,OAAO,GAAG,CAAC,OAA2B,EAAW,EAAE;QACvD,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAA;QAE3B,UAAU;QACV,MAAM,UAAU,GAAG,IAAA,qBAAa,EAAC,OAAO,CAAC,CAAA;QAEzC,wDAAwD;QACxD,MAAM,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAA;QAC1E,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC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EAAW,EAAE;QACvD,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAA;QAE3B,UAAU;QACV,MAAM,UAAU,GAAG,IAAA,qBAAa,EAAC,OAAO,CAAC,CAAA;QAEzC,wDAAwD;QACxD,MAAM,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAA;QAC1E,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAA;QAElD,sCAAsC;QACtC,MAAM,4BAA4B,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAA;QAClF,MAAM,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAA;QAC3D,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAA;QAEjD,YAAY;QACZ,MAAM,OAAO,GACX,qBAAqB,KAAK,WAAW;YACrC,CAAC,IAAI,KAAK,OAAO,IAAI,4BAA4B,KAAK,kBAAkB,CAAC,CAAA;QAE3E,qEAAqE;QACrE,MAAM,iBAAiB,GACrB,SAAS;YACT,CAAC,CAAC,iBAAyB,EAAE,OAAgB,EAAE,EAAE;gBAC/C,OAAO,OAAO,CAAC,OAAO,CAAA;YACxB,CAAC,CAAC,CAAA;QAEJ,SAAS;QACT,uCACK,OAAO,KACV,SAAS,EAAE,iBAAiB,EAC5B,UAAU;YACV,OAAO,IACR;IACH,CAAC,CAAA;IAED,OAAO,IAAA,mBAAY,EAAC;QAClB,IAAI;QACJ,QAAQ;QACR,OAAO;QACP,MAAM,EAAN,eAAM;KACP,CAAC,CAAA;AACJ,CAAC,CAAA;AAEQ,8DAAyB"}
package/dist/types.d.ts CHANGED
@@ -1,14 +1,15 @@
1
1
  import type Stripe from 'stripe';
2
2
  import type { CORSOptions, YogaInitialContext } from '@graphql-yoga/node';
3
3
  export declare type StripeGraphQLContext = {
4
- isAllowed: (stripeCustomerId: string, context: Context) => boolean;
4
+ isAllowed: (stripeCustomerId: string, context: Context) => boolean | Promise<boolean>;
5
5
  userClaims?: UserHasuraClaims;
6
6
  isAdmin: boolean;
7
7
  };
8
8
  export declare type Context = YogaInitialContext & StripeGraphQLContext;
9
9
  export declare type CreateServerProps = {
10
10
  cors?: CORSOptions;
11
- isAllowed?: (stripeCustomerId: string, context: Context) => boolean;
11
+ isAllowed?: (stripeCustomerId: string, context: Context) => boolean | Promise<boolean>;
12
+ graphiql?: boolean;
12
13
  };
13
14
  export declare type StripePaymentMethod = Stripe.PaymentMethod & {
14
15
  customer: string | null;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAEhC,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AAEzE,oBAAY,oBAAoB,GAAG;IACjC,SAAS,EAAE,CAAC,gBAAgB,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAA;IAClE,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,OAAO,EAAE,OAAO,CAAA;CACjB,CAAA;AAED,oBAAY,OAAO,GAAG,kBAAkB,GAAG,oBAAoB,CAAA;AAE/D,oBAAY,iBAAiB,GAAG;IAC9B,IAAI,CAAC,EAAE,WAAW,CAAA;IAClB,SAAS,CAAC,EAAE,CAAC,gBAAgB,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAA;CACpE,CAAA;AAID,oBAAY,mBAAmB,GAAG,MAAM,CAAC,aAAa,GAAG;IACvD,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;CACxB,CAAA;AAED,oBAAY,kBAAkB,GAAG,MAAM,CAAC,YAAY,GAAG;IACrD,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;CAC1B,CAAA;A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rD,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;CAC1B,CAAA;AAED,oBAAY,aAAa,GAAG,MAAM,CAAC,OAAO,GAAG;IAC3C,EAAE,EAAE,MAAM,CAAA;IACV,QAAQ,EAAE,MAAM,CAAA;IAChB,sBAAsB,EAAE,mBAAmB,GAAG,IAAI,CAAA;CACnD,CAAA;AAED,oBAAY,gBAAgB,GAAG;IAC7B,kBAAkB,EAAE,MAAM,CAAA;IAC1B,uBAAuB,EAAE,MAAM,CAAA;IAC/B,wBAAwB,EAAE,MAAM,EAAE,CAAA;CACnC,GAAG;IACF,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CACtB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nhost/stripe-graphql-js",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Stripe GraphQL API",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -53,5 +53,6 @@
53
53
  "lint:fix": "eslint . --ext .ts,.tsx --fix",
54
54
  "verify": "run-p prettier lint",
55
55
  "verify:fix": "run-p prettier:fix lint:fix"
56
- }
56
+ },
57
+ "readme": "<h1 align=\"center\">@nhost/stripe-graphql-js</h1>\n<h2 align=\"center\">Stripe GraphQL API</h2>\n\n<p align=\"center\">\n <img alt=\"npm\" src=\"https://img.shields.io/npm/v/@nhost/stripe-graphql-js\">\n <img alt=\"npm\" src=\"https://img.shields.io/npm/dm/@nhost/stripe-graphql-js\">\n <a href=\"LICENSE\">\n <img src=\"https://img.shields.io/badge/license-MIT-yellow.svg\" alt=\"license: MIT\" />\n </a>\n</p>\n\nThis package creates a Stripe GraphQL API.\n\n```graphql\nquery {\n stripe {\n customer(id: \"cus_xxx\" {\n id\n name\n invoices {\n data {\n id\n created\n paid\n hostedInvoiceUrl\n }\n }\n }\n }\n}\n```\n\nYou can also add the Stripe GraphQL API as a Hasura Remote Schema and connect data from your database and Stripe. This allows you to request data from your database and Stripe in a single GraphQL query:\n\n```graphql\nquery {\n users {\n # User in your database\n id\n displayName\n userData {\n stripeCustomerId # Customer's Stripe Customer Id\n stripeCustomer {\n # Data from Stripe\n id\n name\n paymentMethods {\n id\n card {\n brand\n last4\n }\n }\n }\n }\n }\n}\n```\n\n## Install\n\n```bash\nnpm install @nhost/stripe-graphql-js\n```\n\n## Quick Start\n\n### Serverless Function Setup\n\nCreate a new [Serverless Function](https://docs.nhost.io/platform/serverless-functions) `functions/graphql/stripe.ts`:\n\n```js\nimport { createStripeGraphQLServer } from '@nhost/stripe-graphql-js'\n\nconst server = createStripeGraphQLServer()\n\nexport default server\n```\n\n> You can run the Stripe GraphQL API in any JS environment because it's built using [GraphQL Yoga](https://github.com/dotansimha/graphql-yoga).\n\n### Stripe Secret Key\n\nAdd `STRIPE_SECRET_KEY` as an environment variable. If you're using Nhost, add `STRIPE_SECRET_KEY` to `.env.development` like this:\n\n```\nSTRIPE_SECRET_KEY=sk_test_xxx\n```\n\nLearn more about [Stripe API keys](https://stripe.com/docs/keys#obtain-api-keys).\n\n### Start Nhost\n\n```\nnhost up\n```\n\nLearn more about the [Nhost CLI](https://docs.nhost.io/platform/cli).\n\n### Test\n\nTest the Stripe GraphQL API in the browser:\n\n[http://localhost:1337/v1/functions/graphql/stripe](http://localhost:1337/v1/functions/graphql/stripe)\n\n### Remote Schema\n\nAdd the Stripe GraphQL API as a Remote Schema in Hasura.\n\n**URL**\n\n```\n{{NHOST_BACKEND_URL}}/v1/functions/graphql/stripe\n```\n\n**Headers**\n\n```\nx-nhost-webhook-secret: NHOST_WEBHOOK_SECRET (from env var)\n```\n\n![Hasura Remote Schema](./assets//hasura-remote-schema.png)\n\n## Permissions\n\nHere's a minimal example without any custom permissions. Only requests using the `x-hasura-admin-secret` header will work:\n\n```js\nconst server = createStripeGraphQLServer()\n```\n\nFor 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.\n\nHere is an example of an `isAllowed` function:\n\n```js\n\nconst isAllowed = (stripeCustomerId: string, context: Context) => {\n const { isAdmin, userClaims } = context\n\n // allow requests if it has a valid `x-hasura-admin-secret`\n if (isAdmin) {\n return true\n }\n\n // get user id\n const userId = userClaims['x-hasura-user-id']\n\n // check if user is signed in\n if (!userId) {\n return false;\n }\n\n // get more user information from the database\n const { user } = await gqlSDK.getUser({\n id: userId,\n });\n\n if (!user) {\n return false;\n }\n\n // check if the user is part of a workspace with the `stripeCustomerId`\n return user.workspaceMembers\n .some((workspaceMember) => {\n return workspaceMember.workspace.stripeCustomerId === stripeCustomerId;\n });\n}\n\n```\n\n### Context\n\nThe `context` object contains:\n\n- `userClaims` - verified JWT claims from the user's access token.\n- `isAdmin` - `true` if the request was made using a valid `x-hasura-admin-secret` header.\n- `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\n- `query` - the DocumentNode that was parsed from the GraphQL query string\n- `operationName` - the operation name selected from the incoming query\n- `variables` - the variables that were defined in the query\n- `extensions` - the extensions that were received from the client\n\nRead more about the [default context from GraphQL Yoga](https://www.the-guild.dev/graphql/yoga-server/docs/features/context#default-context).\n\n## Development\n\nInstall dependencies:\n\n```bash\npnpm install\n```\n\nStart the development server:\n\n```bash\npnpm dev\n```\n\nThe GraphQL Server will reload every time the code changes.\n\nOpen GraphiQL:\n\n[http://0.0.0.0:4000/graphql](http://0.0.0.0:4000/graphql)\n"
57
58
  }