preface

Before I started my new job, I was working purely on the front end. Although the current position is also front-end development, but the back-end interface is also written in Node. So I came up with a series of white tips to record my experience.

Node encapsulates the result returned by the interface

demand

Uniform formatting of returned res, including error code and return format. Also conform to typescript syntax validation specifications.

Train of thought

The implementation idea is similar to the secondary encapsulation of AXIos, the unified packaging error handling of returned data, and loading logic. Therefore, I’m going to do general processing on router.use.

implementation

In addition to the total thrown router interface, the middle layer adds the implementation of jsonSuccess and jsonFail

// Router file index.ts
import express from 'express'
import { Response } from '.. /config/types/router-response'
import errorCode from '.. /config/error-code'

const router = express.Router()
router.use((req, res: Response, next) = > {
  res.jsonSuccess = (data): void= > {
    res.json({
      code: 200.message: 'success'.result: data,
    })
  }
  res.jsonFail = (code, message): void= > {
    res.json({
      code,
      msg: errorCode[code] || message,
    })
  }
  next()
})
export default router
Copy the code
/ / the router - respons. Which s file
export * from 'express-serve-static-core'
declare module 'express-serve-static-core' {
  interface Response {
    jsonSuccess: (result: any, code: 200, message: 'success') = > void
    preProcessing: (data: any) = > void
    jsonFail: (code: number, message: string, extra? : any) = > void}}Copy the code
// error-code.ts
export default {
  500: 'Server internal error'.404: 'token failure'. }Copy the code

Using the example

// test.ts
import express from 'express'
import TestModel from '.. /model/test-instance'
const router = express.Router()

router.post('/test'.async (req, res) => {
  try {
    const result = TestModel.update({ test: '123' })
    res.jsonSuccess(result)
  } catch (e) {
    res.jsonFail(500)}})Copy the code

Difficulty miscellaneous points

.d.ts suffix file

What are the functions of files with the suffix. D. ts for the first time in my academic life?

As we all know, TypeScript is a superset of JavaScript. I think the biggest difference between the two is that TS adds strong type verification, which is concentrated in declaring variables, function parameters, and function return values.

Function 1: Some packages lack type checking when TS directly references third-party JS libraries, so we need to make a declaration file to declare the types of these third-party libraries, otherwise esLint will have red flags when verifying rules.

Function two: Need to encapsulate the existing data type twice, add brick to it. (As in my implementation above)

Related TS syntax -declare

Note: In typescript rules, if a.d.ts file does not use the import or export syntax, the topmost declared variable is a global variable

// declare an aTest global variable
declare let aTest: number; 

// Declare a Get interface
declare interface Get{
    id: string: string;
    name:string: string;
}

// Declare an object
declare namespace myObj{
    let age: number;
    function getAge(age: number) :number;
}

// Write a modular type
declare module 'test' {
    const MAXYEAR:400
    export=MAXYEAR
 }
Copy the code