Build a backend with TypeScript using Nest.js RICHARD MOOT DEVELOPER EVANGELIST @ SQUARE
What is NestJS? A progressive Node.js framework for building efficient, reliable and scalable server-side applications. Huh? Nest provides an out-of-the-box application architecture which allows developers and teams to create highly testable, scalable, loosely coupled, and easily maintainable applications. � 3
How is this different that just using Express? Fully supports TypeScript - Structured (similar to Angular) - Extremely modular - Supports dependency injection - Platform agnostic - GraphQL, microservices, CLI, and OpenAPI support - � 4
What does a Nest app look like? $ npm i -g @nestjs/cli $ nest new project-name � 5
app.module.ts � 6
app.service.ts � 7
app.controller.ts � 8
main.ts � 9
Real App Time – Order Ahead � 10
square.service.ts � 11
import { Injectable } from '@nestjs/common'; import { ConfigService } from '../config/config.service'; import * as SquareConnect from 'square-connect'; import { randomBytes } from 'crypto'; import { PaymentDto } from './dto/PaymentDto.dto'; import * as functions from 'firebase-functions'; @Injectable() export class SquareService { constructor(config: ConfigService) { this.accessToken = config.get("ACCESS_TOKEN"); const SquareClient = SquareConnect.ApiClient.instance; const oauth2 = SquareClient.authentications["oauth2"]; oauth2.accessToken = this.accessToken; Footnote � 12 } private accessToken: string;
import { Injectable } from '@nestjs/common'; import { ConfigService } from '../config/config.service'; import * as SquareConnect from 'square-connect'; import { randomBytes } from 'crypto'; import { PaymentDto } from './dto/PaymentDto.dto'; import * as functions from 'firebase-functions'; @Injectable() export class SquareService { constructor(config: ConfigService) { this.accessToken = config.get("ACCESS_TOKEN"); const SquareClient = SquareConnect.ApiClient.instance; const oauth2 = SquareClient.authentications["oauth2"]; oauth2.accessToken = this.accessToken; Footnote � 13 } private accessToken: string;
import { Injectable } from '@nestjs/common'; import { ConfigService } from '../config/config.service'; import * as SquareConnect from 'square-connect'; import { randomBytes } from 'crypto'; import { PaymentDto } from './dto/PaymentDto.dto'; import * as functions from 'firebase-functions'; @Injectable() export class SquareService { constructor(config: ConfigService) { this.accessToken = config.get("ACCESS_TOKEN"); const SquareClient = SquareConnect.ApiClient.instance; const oauth2 = SquareClient.authentications["oauth2"]; oauth2.accessToken = this.accessToken; Footnote � 14 } private accessToken: string;
import { randomBytes } from 'crypto'; import { PaymentDto } from './dto/PaymentDto.dto'; import * as functions from 'firebase-functions'; @Injectable() export class SquareService { constructor(config: ConfigService) { this.accessToken = config.get("ACCESS_TOKEN"); const SquareClient = SquareConnect.ApiClient.instance; const oauth2 = SquareClient.authentications["oauth2"]; oauth2.accessToken = this.accessToken; } private accessToken: string; async pay(paymentRequest: PaymentDto){ const locationsApi = new SquareConnect.LocationsApi(); const transactionsApi = new SquareConnect.TransactionsApi(); const ordersApi = new SquareConnect.OrdersApi(); � 15 const { locations } = await locationsApi.listLocations();
import { randomBytes } from 'crypto'; import { PaymentDto } from './dto/PaymentDto.dto'; import * as functions from 'firebase-functions'; @Injectable() export class SquareService { constructor(config: ConfigService) { this.accessToken = config.get("ACCESS_TOKEN"); const SquareClient = SquareConnect.ApiClient.instance; const oauth2 = SquareClient.authentications["oauth2"]; oauth2.accessToken = this.accessToken; } private accessToken: string; async pay(paymentRequest: PaymentDto){ const locationsApi = new SquareConnect.LocationsApi(); const transactionsApi = new SquareConnect.TransactionsApi(); const ordersApi = new SquareConnect.OrdersApi(); � 16 const { locations } = await locationsApi.listLocations();
import { randomBytes } from 'crypto'; import { PaymentDto } from './dto/PaymentDto.dto'; import * as functions from 'firebase-functions'; @Injectable() export class SquareService { constructor(config: ConfigService) { this.accessToken = config.get("ACCESS_TOKEN"); const SquareClient = SquareConnect.ApiClient.instance; const oauth2 = SquareClient.authentications["oauth2"]; oauth2.accessToken = this.accessToken; } private accessToken: string; async pay(paymentRequest: PaymentDto){ const locationsApi = new SquareConnect.LocationsApi(); const transactionsApi = new SquareConnect.TransactionsApi(); const ordersApi = new SquareConnect.OrdersApi(); � 17 const { locations } = await locationsApi.listLocations();
import { randomBytes } from 'crypto'; import { PaymentDto } from './dto/PaymentDto.dto'; import * as functions from 'firebase-functions'; @Injectable() export class SquareService { constructor(config: ConfigService) { this.accessToken = config.get("ACCESS_TOKEN"); const SquareClient = SquareConnect.ApiClient.instance; const oauth2 = SquareClient.authentications["oauth2"]; oauth2.accessToken = this.accessToken; } private accessToken: string; async pay(paymentRequest: PaymentDto){ const locationsApi = new SquareConnect.LocationsApi(); const transactionsApi = new SquareConnect.TransactionsApi(); const ordersApi = new SquareConnect.OrdersApi(); � 18 const { locations } = await locationsApi.listLocations();
private accessToken: string; async pay(paymentRequest: PaymentDto){ const locationsApi = new SquareConnect.LocationsApi(); const transactionsApi = new SquareConnect.TransactionsApi(); const ordersApi = new SquareConnect.OrdersApi(); const { locations } = await locationsApi.listLocations(); const locationId = locations[0].id; try { const { order } = await ordersApi.createOrder(locationId, { idempotency_key: randomBytes(48).toString('hex'), order: { line_items: [ { quantity: '1', catalog_object_id: paymentRequest.item.variations[0].id, Footnote � 19 } ],
private accessToken: string; async pay(paymentRequest: PaymentDto){ const locationsApi = new SquareConnect.LocationsApi(); const transactionsApi = new SquareConnect.TransactionsApi(); const ordersApi = new SquareConnect.OrdersApi(); const { locations } = await locationsApi.listLocations(); const locationId = locations[0].id; try { const { order } = await ordersApi.createOrder(locationId, { idempotency_key: randomBytes(48).toString('hex'), order: { line_items: [ { quantity: '1', catalog_object_id: paymentRequest.item.variations[0].id, Footnote � 20 } ],
private accessToken: string; async pay(paymentRequest: PaymentDto){ const locationsApi = new SquareConnect.LocationsApi(); const transactionsApi = new SquareConnect.TransactionsApi(); const ordersApi = new SquareConnect.OrdersApi(); const { locations } = await locationsApi.listLocations(); const locationId = locations[0].id; try { const { order } = await ordersApi.createOrder(locationId, { idempotency_key: randomBytes(48).toString('hex'), order: { line_items: [ { quantity: '1', catalog_object_id: paymentRequest.item.variations[0].id, Footnote � 21 } ],
private accessToken: string; async pay(paymentRequest: PaymentDto){ const locationsApi = new SquareConnect.LocationsApi(); const transactionsApi = new SquareConnect.TransactionsApi(); const ordersApi = new SquareConnect.OrdersApi(); const { locations } = await locationsApi.listLocations(); const locationId = locations[0].id; try { const { order } = await ordersApi.createOrder(locationId, { idempotency_key: randomBytes(48).toString('hex'), order: { line_items: [ { quantity: '1', catalog_object_id: paymentRequest.item.variations[0].id, Footnote � 22 } ],
const locationId = locations[0].id; try { const { order } = await ordersApi.createOrder(locationId, { idempotency_key: randomBytes(48).toString('hex'), order: { line_items: [ { quantity: '1', catalog_object_id: paymentRequest.item.variations[0].id, } ], fulfillments: [ { type: 'PICKUP', state: 'PROPOSED', pickup_details: { recipient: { Footnote � 23 display_name: paymentRequest.customer.name, email: paymentRequest.customer.email
const locationId = locations[0].id; try { const { order } = await ordersApi.createOrder(locationId, { idempotency_key: randomBytes(48).toString('hex'), order: { line_items: [ { quantity: '1', catalog_object_id: paymentRequest.item.variations[0].id, } ], fulfillments: [ { type: 'PICKUP', state: 'PROPOSED', pickup_details: { recipient: { Footnote � 24 display_name: paymentRequest.customer.name, email: paymentRequest.customer.email
const locationId = locations[0].id; try { const { order } = await ordersApi.createOrder(locationId, { idempotency_key: randomBytes(48).toString('hex'), order: { line_items: [ { quantity: '1', catalog_object_id: paymentRequest.item.variations[0].id, } ], fulfillments: [ { type: 'PICKUP', state: 'PROPOSED', pickup_details: { recipient: { Footnote � 25 display_name: paymentRequest.customer.name, email: paymentRequest.customer.email
Recommend
More recommend