How to use @foal/core - 10 common examples

To help you get started, we’ve selected a few @foal/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github FoalTS / foal / packages / acceptance-tests / src / examples / validation-and-sanitization.spec.ts View on Github external
it('Usage with a Hook', () => {
      class SocialPost {

        @Length(10, 20)
        title: string;

        @Contains('hello')
        text: string;

      }

      class SocialPostController {

        @Post()
        @ValidateBodyFromClass(SocialPost, { /* options if relevant */ })
        createSocialPost() {
          // ...
          return new HttpResponseCreated();
        }

      }

      const app = createApp(SocialPostController);

      return request(app)
        .post('/')
        .send({ text: 'foo' })
        .expect(400)
        .expect([
          {
github FoalTS / foal / packages / acceptance-tests / src / examples / validation-and-sanitization.spec.ts View on Github external
describe('Validation & Sanitization of HTTP Requests', () => {

      // Test compilation
      // tslint:disable-next-line:no-unused-variable
      class MyController {

        @Post('/user')
        @ValidateBody({
          additionalProperties: false,
          properties: {
            firstName: { type: 'string' },
            lastName: { type: 'string' },
          },
          required: [ 'firstName', 'lastName' ],
          type: 'object'
        })
        postUser(ctx: Context) {
          // In this method we are sure that firstName and lastName
          // are defined thanks to the above hook.
          console.log(
            ctx.request.body.firstName, ctx.request.body.lastName
          );
          return new HttpResponseOK();
        }
github FoalTS / foal / packages / acceptance-tests / src / csrf / regular-web-app.stateless.spec.ts View on Github external
async home(ctx: Context) {
      // Normally in an HTML template
      const response = new HttpResponseOK();
      setCsrfCookie(response, await getCsrfToken());
      return response;
    }
  }
github FoalTS / foal / packages / acceptance-tests / src / openapi.hooks.spec.ts View on Github external
const actualDocument = createOpenApiDocument(ApiController);

    deepStrictEqual(actualDocument, expectedDocument);

    // Test hook conflicts (Two calls of @JWT).

    @ApiInfo({
      title: 'My API',
      version: '1.0.0'
    })
    class ApiController2 {
      @Get('/products')
      @JWTOptional()
      readProducts() {}

      @Post('/products')
      @JWTRequired()
      @ValidateBody({
        properties: {
          name: { type: 'string' }
        },
        type: 'object',
      })
      createProduct() {}
    }

    const yamlDocument2 = readFileSync(join(__dirname, './assets/openapi.hooks2.yml'), 'utf8');
    const expectedDocument2 = parse(yamlDocument2);

    const actualDocument2 = createOpenApiDocument(ApiController2);

    deepStrictEqual(actualDocument2, expectedDocument2);
github FoalTS / foal / packages / acceptance-tests / src / auth.typeorm.spec.ts View on Github external
class AppController {
      subControllers = [
        MyController,
        AuthController
      ];
    }

    await createConnection({
      database: 'e2e_db.sqlite',
      dropSchema: true,
      entities: [ User, Permission, Group ],
      synchronize: true,
      type: 'sqlite',
    });

    const app = createApp(AppController);

    /* Create a user */

    await createConnection({
      database: 'e2e_db.sqlite',
      dropSchema: true,
      entities: [ User, Permission, Group ],
      name: 'create-connection',
      synchronize: true,
      type: 'sqlite',
    });

    const user = new User();
    user.email = 'john@foalts.org';
    user.password = await hashPassword('password');
    await getRepository(User, 'create-connection').save(user);
github FoalTS / foal / packages / acceptance-tests / src / examples / validation-and-sanitization.spec.ts View on Github external
text: string;

      }

      class SocialPostController {

        @Post()
        @ValidateBodyFromClass(SocialPost, { /* options if relevant */ })
        createSocialPost() {
          // ...
          return new HttpResponseCreated();
        }

      }

      const app = createApp(SocialPostController);

      return request(app)
        .post('/')
        .send({ text: 'foo' })
        .expect(400)
        .expect([
          {
            children: [],
            constraints: { length: 'title must be longer than or equal to 10 characters' },
            property: 'title',
            target: { text: 'foo' },
          },
          {
            children: [],
            constraints: { contains: 'text must contain a hello string' },
            property: 'text',
github FoalTS / foal / packages / acceptance-tests / src / mongoose-db.redis-store.spec.ts View on Github external
password: string;
    isAdmin: boolean;
  }

  // We need to call the model 'User2' here not to conflict with another test in this package.
  const UserModel: Model = model('User2', UserSchema);

  function AdminRequired() {
    return Hook((ctx: Context) => {
      if (!ctx.user.isAdmin) {
        return new HttpResponseForbidden();
      }
    });
  }

  @TokenRequired({ user: fetchUser(UserModel), store: RedisStore })
  class MyController {
    @Get('/foo')
    foo() {
      return new HttpResponseOK();
    }

    @Get('/bar')
    @AdminRequired()
    bar() {
      return new HttpResponseOK();
    }
  }

  class AuthController {
    @dependency
    store: RedisStore;
github FoalTS / foal / packages / acceptance-tests / src / csrf / regular-web-app.stateful.spec.ts View on Github external
}
  }

  @TokenRequired({
    cookie: true,
    store: TypeORMStore,
  })
  @CsrfTokenRequired()
  class ApiController {
    @Post('/products')
    createProduct() {
      return new HttpResponseCreated();
    }
  }

  @TokenRequired({
    cookie: true,
    redirectTo: '/login',
    store: TypeORMStore,
  })
  class PageController {
    @Get('/home')
    async home(ctx: Context) {
      // Normally in an HTML template
      return new HttpResponseOK({ csrfToken: await getCsrfToken(ctx.session) });
    }
  }

  class AppController {
    subControllers = [
      AuthController,
      PageController,
github FoalTS / foal / samples / tutorials / nuxt.js / backend / src / index.ts View on Github external
async function main() {
  // Init Nuxt.js
  const nuxt = new Nuxt(config);

  // Build only in dev mode
  if (config.dev) {
    const builder = new Builder(nuxt);
    await builder.build();
  } else {
    await nuxt.ready();
  }

  await createConnection();

  const app = createApp(AppController, {
    postMiddlewares: [
      nuxt.render
    ]
  });

  const httpServer = http.createServer(app);
  const port = Config.get('port', 3001);
  httpServer.listen(port, () => {
    console.log(`Listening on port ${port}...`);
  });
}
github FoalTS / foal / packages / swagger / src / swagger-controller.spec.ts View on Github external
it('should redirect the user to xxx/ if there is no trailing slash in the URL.', async () => {
      // This way, the browser requests the assets at the correct path (the relative path).
      const controller = new ConcreteClass();

      const ctx = new Context({ path: 'xxx' });
      const response = await controller.index(ctx);

      if (!isHttpResponseMovedPermanently(response)) {
        throw new Error('SwaggerController.index should return an HttpResponseMovedPermanently instance.');
      }

      strictEqual(response.path, ctx.request.path + '/');
    });