Git Product home page Git Product logo

nfw's Introduction

alt text

NFW a node Typescript boilerplate

Test Lint CodeQL

This repository contains a JSON-API REST API boilerplate using NFW-CORE.

โš ๏ธ : ESM only, no CommonJS modules.

Requirements

  • Typescript >= 4.9.x (waiting on tsyringe to move to 5.x)
  • node >= 18.x
  • pnpm 8.x

Install

Any package manager should do the trick but i recommend using Pnpm.

pnpm i

Install the database container.

docker compose up -d

Environments

You must create a config/env/<NODE_ENV>.env file for each env at the root of your project.

The structure of the env file is validated and can be found in the src/api/services/configuration.service.ts service.

Scripts

Start from dev env

pnpm start:dev

Start from test env

Useful for debugging

pnpm start:test
pnpm mikro-orm:cli <any command>

Production and deployments

You need to transpile (or bundle) the Typescript. And then run node against it. it's up to you.

pnpm tsc
# rollup ...
# docker containers ...

Tests

Runs the tests with vitest. The migrations are run and database is cleared before testing.

pnpm test

With beautiful UI in watch mode and coverage

pnpm test -- --ui --watch --coverage

In watch mode

pnpm test -- --watch

File structure

  • config: config files (some config files that cannot be moved stay in root)
  • database: the docker database init files.
  • dist: the typescript output folder
  • src
    • api: transport and configuration related files.
    • database: database and ORM related files.
  • tests:
    • mocks: mocks folder
    • src: the test files folder
      • acceptance: acceptance tests files
      • integration: integration tests files
      • unit: unit tests files
    • static: static files (png,pdf, ...) for testing
    • utils: utils for testing

Notes

  • App must not depend on tests folder.
  • Path aliases are used in typescript to have clearer imports and separation. You cannot import app into app. Import must be relative when the import in the same path.

nfw's People

Contributors

amauryd avatar baptiste-dmbn avatar dramixdw avatar gilles-bertrand avatar remadex avatar snorkell avatar steve-lebleu avatar teaflex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nfw's Issues

classify services

global service container

  • OAuth
  • Cache

config classify

  • Passport
  • Multer
  • Logger
  • Env

ACL & security

Use an ACL module to authorize GET / LIST / CREATE / DELETE / UPDATE for resources

Improve the serializers

We should consider improving the serializing/de-serializing process. They are not generic enough

Param order matter when it should not

Reproduce issue:

  @JsonApiGet()
  async get (@Param('id') id: string, query: JsonApiQuery, @CurrentUser() currentUser: UserModel) {
    const document = await this.documentService.getOneOrFail(id, query);
    await canOrFail(this.authorizer, currentUser, 'read', [document])
    return this.registry.getSerializerFor<DocumentResource>('documents').serializeOne(document);
  }

This create an error because query become currentUser.

  @JsonApiGet()
  async get (@Param('id') id: string, @JsonApiQueryDecorator(RESOURCE_NAME) query: JsonApiQuery, @CurrentUser() currentUser: UserModel) {
    const document = await this.documentService.getOneOrFail(id, query);
    await canOrFail(this.authorizer, currentUser, 'read', [document])
    return this.registry.getSerializerFor<DocumentResource>('documents').serializeOne(document);
  }

This does not create an error because currentUser is correctly assigned

Expected:

Decorator to assign the param assigned to it and not the param before

problem with pagination and sorting combined

typeorm/typeorm#2912

Workaround : don't escape string / use parent alias before attribute

File : https://github.com/TRIPTYK/nfw/blob/develop/src/core/repositories/base.repository.ts

query parameter sort=name

if (allowSorting && query.sort) {
            const sortFields = splitAndFilter(query.sort, ","); // split parameters and filter empty strings

            // need to use SqlString.escapeId in order to prevent SQL injection on orderBy()
            for (const field of sortFields) {
                if (field[0] === "-") {  // JSON-API convention , when sort field starts with '-' order is DESC
                    queryBuilder.orderBy(SqlString.escapeId(field.substr(1)), "DESC");
                } else {
                    queryBuilder.orderBy(SqlString.escapeId(field), "ASC");
                }
            }
        }

TO

query parameter sort=<entity>.name

        if (allowSorting && query.sort) {
            const sortFields = splitAndFilter(query.sort, ","); // split parameters and filter empty string

            // need to use SqlString.escapeId in order to prevent SQL injection on orderBy()
            for (const field of sortFields)
            {
                if (field[0] === "-") {  // JSON-API convention , when sort field starts with '-' order is DESC
                    queryBuilder.addOrderBy(field.substr(1), "DESC");
                } else {
                    queryBuilder.addOrderBy(field, "ASC");
                }
            }
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.