Coder Social home page Coder Social logo

mattburnett-repo / nestjs-api-template Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 1.46 MB

Template for building REST APIs with NestJS

Home Page: https://nestjs-api-template.fly.dev/api

JavaScript 1.13% Shell 0.46% TypeScript 97.58% Dockerfile 0.83%
api api-rest nestjs nestjs-starter-template template-project

nestjs-api-template's Introduction

Hi there ๐Ÿ‘‹

  • ๐Ÿ”ญ Iโ€™m currently working on activist.org (Vue + TypeScript + Django).
  • ๐ŸŒฑ Iโ€™m currently learning the Hugging Face ecosystem.
  • ๐Ÿ‘ฏ Iโ€™m looking to collaborate on projects that are socially beneficial and/or climate positive.

My interest in web programming / development started in the early 90's, when I discovered how to author Windows 3.1 help files. The idea of hypertext opened up a world of possibilities.

Since then I have followed interests that pursue the idea of capturing, processing and presenting information. For 15 years I worked as a developer during the Web 1.0 era, in the US government space. I ended up taking a break from that to pursue other interests, and after a relocation to Berlin I upskilled a few years ago to the current web development technologies.

My main interest is in making things that are useful, and making things that already exist better.

Personal / portfolio website is here.

Blog is here.

nestjs-api-template's People

Contributors

mattburnett-repo avatar

Watchers

 avatar

nestjs-api-template's Issues

Improve auth

Passport / Authentication.

Roles / authorization

  • CASL / RBAC

More / improved testing

  • More edge case tests are always better.
  • app.e2e-spec.ts
    • reliably turn off logging output
  • auth.e2e-spec.ts
    • figure out how to either attach req.user, or how to mock req.user.
      • underlying refresh / logout code expects this. Tests can't run witout @Req / req.user object.

About the req.user issue, here is a suggestion from chatGPT:

  1. First, create a test case and add a cookie to the HTTP request:
    it('should return "Hello World!" with a user object', () => {
    const user = {
    name: 'John Doe',
    email: '[email protected]',
    id: '123456789',
    };

return request(app.getHttpServer())
.get('/my-route')
.set('Cookie', user=${JSON.stringify(user)})
.expect(200)
.then(response => {
expect(response.text).toBe('Hello World!');
});
});

In this example, we've added a user cookie to the HTTP request with the user object as its value. We've also converted the user object to a JSON string using JSON.stringify() to make it easier to pass it in the cookie.

  1. In your controller, extract the user object from the request object using the @Req() decorator:

import { Controller, Get, Req } from '@nestjs/common';
import { Request } from 'express';

@controller()
export class MyController {
@get('/my-route')
myRoute(@Req() request: Request): string {
const user = JSON.parse(request.cookies['user']);
console.log(user); // { name: 'John Doe', email: '[email protected]', id: '123456789' }
return 'Hello World!';
}
}

In this example, we're using the @Req() decorator to inject the request object into the myRoute() method of the MyController class. We're then using JSON.parse() to convert the user cookie value back into a JavaScript object using the request.cookies object. This gives us access to the req.user object in our controller, which we can then use as needed.

Note: You can also use the set() method to set a header with the user object as its value, similar to the first example I provided. The approach you take will depend on your use case and preference.

Refactor token acquisition in app.e2e-spec.ts

For the 'GETs a protected endpoint when auth is provided.' test:

  • mock the call to /auth/login. We don't need to call the database here. We just need to get a token from this.authService.login(data)
  • refactor the loginResponse block to a BeforeAll hook, so that we can reuse it in future tests.

linting / prettier

there is an issue with 'no empty functions' or something similar.

probably due to an eslint / prettier conflict.

figure out how to config eslint / prettier to work better together (plugins, config, etc.)

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.