Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-spark

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-spark

Koa application boilerplate-less

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
2
Weekly downloads
 
Created
Source

KoaSpark - Koa application boilerplate-less

Build Koa applications in a spark.

KoaSpark is a boilerplate-as-a-library for creating Koa apps, it includes out-of-the-box many koa plugins that you normally need for an app like koa-body, koa-cors and koa-router, plus many other useful features like the automatic generation of OpenAPI definitions.

All of this is typically found in templates and boilerplates via cloning a base git repo and then editing it or via a CLI that generates code, this approach works but keep your app updated when a new version of the template is released is a nightmare.

KoaSpark has a differnet approach, it doesn't generate any code, it's just an importable library that provides an alternative to const app = new Koa(): const app = createApp(config, options) and you have a KoaApp ready to use.

Get Started

npm install koa-spark
# or
yarn add koa-spark

Then create the app:

import { createApp } from 'koa-spark'

const app = createApp()

if (require.main === module) {
  app.startServer()
}

Configuration

In Koa Spark, there are 2 levels of configuration, at the app level AppConfig and at the framework level SparkOptions. They are both passed to the createApp(AppConfig, SparkOptions) function.

App Configuration

The app configuration holds environment-specific configurations, like secrets, connection info, etc. that should be changed by the final user if you redistribute the app, or changed when you deploy in production.

The AppConfig can be configured via environmental variables and/or .env files, Koa Spark will automatically initialize the AppConfig properties decorated with @Field() with the corresponding environment variable. Note that AppConfig properties are written in camelCase but env variables are written in UPPER_SNAKE_CASE.

// config.ts
import { AppConfig, Field } from 'koa-spark'

class MyAppConfig extends AppConfig {
  @Field({ default: 'test@localhost' }) // env: FROM_ADDRESS
  fromAddress!: string

  @Field() // env: GCLOUD_STORAGE_BUCKET
  gcloudStorageBucket!: string

  @Field({ default: 'mongodb://localhost/thedb' })
  mongodbConnectionUri!: string

  @Field()
  sendgridApiKey!: string
}

export default new MyAppConfig()

And its relative .env file:

# .env
FROM_ADDRESS=luca@mail.com
GCLOUD_STORAGE_BUCKET=my_bucket_production

You can have multiple .env files, one per each environment, named .env.<NODE_ENV>. Koa Spark will try to load the environment-specific file and fallback to the generic one, for example in production will try to load .env.production and if it's missing will load .env.

Framework Options

The framework options SparkOptions are used to configure Koa Spark itself, are more inner-configs than AppConfig, for example they include configurations for CORS, helmet, etc.

As for their nature, framework options are not configurable via env variables, but only via code:

const app = createApp(
  config,
  {
    body: {
      multipart: true
    }
  }
)

FAQs

Package last updated on 28 Jan 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc