You can structure your Node.js code in modules where each module's types are in type.ts file.
For example:
**src/modules/devices/types.ts**
export interface Route {
timestamp: string
name: string
geojsonString: string
}
export type AddRouteBodyParams = Pick<Route, 'name' | 'geojsonString'>
export interface DeleteRouteBodyParams {
deviceId: string
timestamp: string
}
export interface EditRouteBodyParams {
deviceId: string
timestamp: string
name: string
geojsonString: string
}
**src/modules/devices/controllerDevice.ts**
import type { Request, Response } from 'express'
import type { ParsedQs } from 'qs'
import type { ResponseError, ResponseSuccess } from '../../api-types'
import { editRoute } from './serviceRoutes'
import type { EditRouteBodyParams } from './types'
export const deviceController = {
editRoute: async (
req: Request<any, any, EditRouteBodyParams, ParsedQs, Record<string, any>>,
res: Response<ResponseSuccess | ResponseError>
) => {
const editResponse = await editRoute({ ...req.body })
if (editResponse instanceof Error) {
return res.status(500).json({ error: editResponse.message })
}
return res.send({ message: `Route with name: ${req.body.name} was updated` })
},
} as const
Then, you can export all api types into a single file and copy it into front-end project by running npm command:
"scripts": {
"generate-types": "tsc -p tsconfig-export.json && cp ./dist/export-api-types.d.ts ../client_map_app/src/types"
}
You can check more details [here][1].
[1]: https://andrejgajdos.com/typescript-type-sharing-between-react-and-node-js/
You can structure your Node.js code in modules where each module's types are in type.ts file.
For example:
**src/modules/devices/types.ts**
export interface Route {
timestamp: string
name: string
geojsonString: string
}
export type AddRouteBodyParams = Pick<Route, 'name' | 'geojsonString'>
export interface DeleteRouteBodyParams {
deviceId: string
timestamp: string
}
export interface EditRouteBodyParams {
deviceId: string
timestamp: string
name: string
geojsonString: string
}
**src/modules/devices/controllerDevice.ts**
import type { Request, Response } from 'express'
import type { ParsedQs } from 'qs'
import type { ResponseError, ResponseSuccess } from '../../api-types'
import { editRoute } from './serviceRoutes'
import type { EditRouteBodyParams } from './types'
export const deviceController = {
editRoute: async (
req: Request<any, any, EditRouteBodyParams, ParsedQs, Record<string, any>>,
res: Response<ResponseSuccess | ResponseError>
) => {
const editResponse = await editRoute({ ...req.body })
if (editResponse instanceof Error) {
return res.status(500).json({ error: editResponse.message })
}
return res.send({ message: `Route with name: ${req.body.name} was updated` })
},
} as const
Then, you can export all api types into a single file and copy it into front-end project by running npm command:
"scripts": {
"generate-types": "tsc -p tsconfig-export.json && cp ./dist/export-api-types.d.ts ../client_map_app/src/types"
}
You can check more details [here][1].
[1]: https://andrejgajdos.com/typescript-type-sharing-between-react-and-node-js/