local-request-annotation
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

local-request-annotation

This is a tool that simulates local code calls in the form of HTTP requests, which can call local code same as send HTTP requests.

Installing

For the latest stable version, run:

npm

npm install local-request-annotation

yarn

yarn add local-request-annotation

pnpm

pnpm add local-request-annotation

bun

bun add local-request-annotation

Usage

when you start, you need to create controller to resolve calls, such as:

import { PostMapping, RequestMapping } from "local-request-annotation";

@RequestMapping('/test')
class TestController {

    @PostMapping('/hello')
    test(query: any, param: any) {
        return { msg: 'hello', param, query }
    }

    @PostMapping('/good')
    goods(query: any, param: any) {
        return { msg: 'goods', param, query }
    }
}

export { TestController }

It is sames like spring, you need to add annotation to register controller and method for the route path, and then use it in your code. When you want to get the request details, you can through arguments of method, it will same as HTTP request, query will get url search query and param will get request body, It's worth noting that you'll need to assert the argument and the type returned, and you won't be able to infer the type even with Typescript.

import LocalHTTPRequest from 'local-request-annotation'
import * as Controllers from './test.ts'

const http = new LocalHTTPRequest(Object.values(Controllers))

http._request({
    url: '/test/hello?b=2',
    data: {a: 1}
}).then(res => {
    console.log(res)
})

It also supports dynamic import, such as import('./test.ts'),

const http = new LocalHTTPRequest()
const Controllers = await import('./test.ts');
http.loadController(Object.values(Controllers))

loadController allows you to load controller dynamically,either way, you must import the corresponding controller before calling the _request method, otherwise you will get undefined.

Returns

when you call _request method, it will return an object,

interface Response {
    data: any,
    headers: any,
    status: number,
    statusText: string,
    config
}

Its sames like axios returns, because it was designed to be mixed with axios, and the returns of controller's method will be used as the data attribute of the response.

The config attribute is the argument of _request method.

Readme

Keywords

none

Package Sidebar

Install

npm i local-request-annotation

Weekly Downloads

4

Version

1.0.3

License

ISC

Unpacked Size

60.3 kB

Total Files

12

Last publish

Collaborators

  • kokomashiro