Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions e-commerce/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
build
coverage
.vscode
.DS_STORE
.env
tmp
8,485 changes: 8,485 additions & 0 deletions e-commerce/api/package-lock.json

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions e-commerce/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "api",
"version": "1.0.0",
"private": true,
"main": "server.ts",
"scripts": {
"dev": "npx tensei serve --watch",
"build": "npx tensei build --production",
"start": "node build/index.js"
},
"devDependencies": {
"@tensei/cli": "^0.9.9",
"typescript": "^4.3.5"
},
"dependencies": {
"@mikro-orm/sqlite": "^4.5.9",
"@tensei/auth": "^0.9.9",
"@tensei/core": "^0.9.9",
"@tensei/graphql": "^0.9.9",
"stripe": "^8.184.0"
}
}
65 changes: 65 additions & 0 deletions e-commerce/api/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Stripe } from 'stripe'
import { ProductModel } from '@tensei/orm'
import { auth } from '@tensei/auth'
import { graphql } from '@tensei/graphql'
import { welcome, tensei, cors, resource, text, textarea, integer, graphQlQuery } from '@tensei/core'

const stripe = new Stripe('sk_test_51JjsxtJnZnb1n2GnbebKQgYe73xTZtT1147z1dLFU9CGmkfbZKvcrW1VX4jA10Wav82slFIGjp9AW6puPURjibMP00z7Fw14Em', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use process.env.STRIPE_SECRET here. The article should note that this key will be set in environment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted

apiVersion: '2020-08-27'
})

interface ProductCartItem {
productId: string
quantity: number
}

export default tensei()
.root(__dirname)
.graphQlTypeDefs([`
input ProductCartItem {
productId: ID
quantity: Int
}

extend type Mutation {
createPaymentIntent(productCartItems: [ProductCartItem]!): String!
}
`])
.graphQlQueries([
graphQlQuery('Create payment intent')
.path('createPaymentIntent')
.mutation()
.handle(async (src, args, ctx, info) => {
const products = await ctx.repositories.products().find({
id: {
$in: ctx.body.productCartItems.map((item: ProductCartItem) => item.productId)
}
})

const totalPrice = products.reduce((total: number, product: ProductModel) => total + (product.price), 0)

const paymentIntent = await stripe.paymentIntents.create({
amount: totalPrice * 100,
currency: 'usd'
})

return paymentIntent.client_secret
})
])
.resources([
resource('Product')
.fields([
text('Image'),
text('Name'),
integer('Price'),
textarea('Description')
])
])
.plugins([
welcome(),
auth().plugin(),
graphql().plugin(),
cors()
])
.start()
.catch(console.error)
Binary file added e-commerce/api/tensei.sqlite
Binary file not shown.
22 changes: 22 additions & 0 deletions e-commerce/api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"include": [
"**/*"
],
"moduleResolution": "node",
"exclude": [
"node_modules",
"build"
],
"compilerOptions": {
"outDir": "build",
"rootDir": "./",
"sourceMap": true,
"target": "ES2020",
"strict": true,
"baseUrl": "./",
"module": "commonjs",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
Loading