Socket
Socket
Sign inDemoInstall

@agape/api

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @agape/api

Framework for building Rest APIs


Version published
Weekly downloads
3
increased by200%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Agape API

Framework for building APIs

Synopsis

@Service()
class FooService {
    foo() {
        return { "message": "BARRRRGGHHHHH" }
    }
}

@Controller()
class FooController {

    constructor( public service: FooService ) {

    }

    @Get('foo')
    foo() {
        return this.service.foo()
    }
}

@Module({
    controllers: [FooController]
})
class FooModule() { }


const app: Application = express();
app.use( express.json() )
app.use( express.urlencoded({ extended: true }))

const router = Router()
bootstrapExpress(router, FooModule)

app.use('/api', router )

Summary

Model-View-Controller APIs

Application Components

There are three primary components to an API application. Modules, Controllers, and Services.

Controllers

Controllers respond to routed requests by interacting with services and returning a response to be sent back to the end user. The Controller class decorator is used to designate a class as a controller.

Class Decorator

Controller

Example
@Controller('path/to/controller')
class FooController {
    ...
}
Method Decorators

One method decorator for each HTTP request.

@Get(path)

@Post(path)

@Put(path)

@Patch(path)

@Delete(path)

@Status(code)

Set a custom success status code. Default is 200 OK.

@Get('teapot')
@Status(418)
teapot() {
    ...
}

@Respond(model)

Example
@Controller('foos')
class FoosController {

    @Get()
    list() {

    }

    @Post()
    create( params: undefined, body: IEvent ) {
        ...
    }

    @Get(':id')
    retrieve( params: { id: string } ) {
        ...
    }

    @Put(':id')
    update( params: { id: string }, body: IEvent ) {
        ...
    }

    @Patch(':id')
    update( params: { id: string }, body: Partial<IEvent> ) {
        ...
    }

    @Delete(':id')
    retrieve( params: { id: string } ) {
        ...
    }

}

Services

Services are injected into controllers and other services through the contstructor.

Declare a service using the Service decorator. Only one instance of a service is used across the entire application via dependency injection.

Class Decorator

Service

Example
@Service()
class FooService {
    
    foo() {
        ...
    }

}

@Service() 
class BarService {
    constructor( public foo: FooService ) {

    }
}

Modules

Modules contain one or more controllers. Controllers need to be declared in a module to be accessible through the API. Declaring a service as part of a module through the provides parameter is optional.

Class Decorator

Module

Parameters

controllers

modules

path

Example
@Module({
    'controllers': [ FooController ],
    'modules': [ BarModule ]
})

Requisites

You must have the compiler options experimentalDecorators and emitDecoratorMetadata set to true in your tsconfig.json file. This means that you cannot bundle with esbuild which does not support these compiler options.

See Also

Express

Author

Maverik Minett maverik.minett@gmail.com

© 2023 Maverik Minett

License

MIT

Keywords

FAQs

Last updated on 19 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc