Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V2] Replace Config.get with Config.get2 #732

Merged
merged 9 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/api-section/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,19 +276,23 @@ class ResolverService {

`formatError` and `@FormatError` also accept an optional parameter to override its default behavior. It is a function that takes the error thrown or rejected in the resolver and return the error that must be sent to the client. It may be asynchronous or synchronous.

> warning: version 2

By default, this function is:
```typescript
function maskAndLogError(err: any): any {
console.log(err);

if (Config.get('settings.debug')) {
if (Config.get('settings.debug', 'boolean')) {
return err;
}

return new Error('Internal Server Error');
}
```

> warning: version 2

But we can also imagine other implementations such as:
```typescript
import { reportErrorTo3rdPartyService } from 'somewhere';
Expand All @@ -304,7 +308,7 @@ async function maskAndLogError(err: any): Promise<any> {
return err;
}

if (Config.get('settings.debug')) {
if (Config.get('settings.debug', 'boolean')) {
return err;
}

Expand Down
20 changes: 10 additions & 10 deletions docs/authentication-and-access-control/jwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const token = sign(
id: 90485234,
email: 'mary@foalts.org'
},
Config.get<string>('settings.jwt.secretOrPublicKey'),
Config.getOrThrow('settings.jwt.secretOrPublicKey', 'string'),
{ expiresIn: '1h' }
);
```
Expand Down Expand Up @@ -113,7 +113,7 @@ export class LoginController {

const token = sign(
{ email: user.email },
Config.get<string>('settings.jwt.secretOrPublicKey'),
Config.getOrThrow('settings.jwt.secretOrPublicKey', 'string'),
{ expiresIn: '1h' }
);

Expand Down Expand Up @@ -238,7 +238,7 @@ export function RefreshJWT(): HookDecorator {
// id: ctx.user.id,
// sub: ctx.user.subject,
},
Config.get<string>('settings.jwt.secretOrPublicKey'),
Config.getOrThrow('settings.jwt.secretOrPublicKey', 'string'),
{ expiresIn: '15m' }
);
response.setHeader('Authorization', newToken);
Expand Down Expand Up @@ -276,7 +276,7 @@ In these cases, the two hooks `JWTRequired` and `JWTOptional` offer a `user` opt
id: 90485234,
email: 'mary@foalts.org'
},
Config.get<string>('settings.jwt.secretOrPublicKey'),
Config.getOrThrow('settings.jwt.secretOrPublicKey', 'string'),
{ expiresIn: '1h' }
);
```
Expand Down Expand Up @@ -447,7 +447,7 @@ const token = sign(
{
email: 'john@foalts.org'
},
Config.get<string>('jwt.privateKey'),
Config.getOrThrow('jwt.privateKey', 'string'),
{ expiresIn: '1h' }
);
```
Expand Down Expand Up @@ -588,8 +588,8 @@ import { JWTRequired } from '@foal/jwt';
// These lines assume that you provided your DOMAIN and AUDIENCE in either
// an .env file, in environment variables or in one the configuration file
// in `config/`.
const domain = Config.get('auth0.domain');
const audience = Config.get('auth0.audience');
const domain = Config.getOrThrow('auth0.domain', 'string');
const audience = Config.getOrThrow('auth0.audience', 'string');

@JWTRequired({
secretOrPublicKey: getRSAPublicKeyFromJWKS({
Expand All @@ -616,9 +616,9 @@ import { JWTRequired } from '@foal/jwt';
// These lines assume that you provided your CLIENT_ID, DOMAIN and USER_POOL_ID
// in either an .env file, in environment variables or in one the configuration
// file in `config/`.
const clientId = Config.get('cognito.clientId');
const domain = Config.get('cognito.domain');
const userPoolId = Config.get('cognito.userPoolId');
const clientId = Config.getOrThrow('cognito.clientId', 'string');
const domain = Config.getOrThrow('cognito.domain', 'string');
const userPoolId = Config.getOrThrow('cognito.userPoolId', 'string');

@JWTRequired({
secretOrPublicKey: getRSAPublicKeyFromJWKS({
Expand Down
2 changes: 1 addition & 1 deletion docs/authentication-and-access-control/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export class AuthController {
email: user.email,
id: user.id,
};
const secret = Config.get<string>('settings.jwt.secretOrPublicKey');
const secret = Config.getOrThrow('settings.jwt.secretOrPublicKey', 'string');

const token = await new Promise<string>((resolve, reject) => {
sign(payload, secret, { subject: user.id.toString() }, (err, value: string) => {
Expand Down
26 changes: 16 additions & 10 deletions docs/cloud/aws-beanstalk.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ npm uninstall sqlite3 connect-sqlite3

### Configure the Database Credentials

> warning: version 2

Replace your `ormconfig.js` (or `ormconfig.yml` or `ormconfig.json`) file with this one:

*ormconfig.js*
```js
const { Config } = require('@foal/core');

module.exports = {
type: Config.get('database.type'),
url: Config.get('database.url'),
database: process.env.RDS_DB_NAME || Config.get('database.name'),
port: process.env.RDS_PORT || Config.get('database.port'),
host: process.env.RDS_HOSTNAME || Config.get('database.host'),
username: process.env.RDS_USERNAME || Config.get('database.username'),
password: process.env.RDS_PASSWORD || Config.get('database.password'),
type: Config.get('database.type', 'string'),
url: Config.get('database.url', 'string'),
database: process.env.RDS_DB_NAME || Config.get('database.name', 'string'),
port: process.env.RDS_PORT || Config.get('database.port', 'number'),
host: process.env.RDS_HOSTNAME || Config.get('database.host', 'string'),
username: process.env.RDS_USERNAME || Config.get('database.username', 'string'),
password: process.env.RDS_PASSWORD || Config.get('database.password', 'string'),
entities: ["build/app/**/*.entity.js"],
migrations: ["build/migrations/*.js"],
cli: {
"migrationsDir": "src/migrations"
},
synchronize: Config.get('database.synchronize')
synchronize: Config.get('database.synchronize', 'boolean')
};

```
Expand Down Expand Up @@ -63,6 +65,8 @@ And complete your configuration file `config/default.json` (or `config/default.y

#### Case 1: The application does not use sessions

> warning: version 2

If you do not use sessions, then remove the store import and the store option from the `createApp` function in the `src/index.ts` file.

```typescript
Expand All @@ -86,7 +90,7 @@ async function main() {
const app = createApp(AppController);

const httpServer = http.createServer(app);
const port = Config.get('port', 3001);
const port = Config.get('port', 'number', 3001);
httpServer.listen(port, () => {
console.log(`Listening on port ${port}...`);
});
Expand All @@ -98,6 +102,8 @@ main();

#### Case 2: The application uses sessions

> warning: version 2

If your application uses sessions, you need to provide a [session store](https://github.com/expressjs/session#compatible-session-stores).

Here is an example with [connect-redis](https://www.npmjs.com/package/connect-redis):
Expand All @@ -124,7 +130,7 @@ async function main() {
});

const httpServer = http.createServer(app);
const port = Config.get('port', 3001);
const port = Config.get('port', 'number', 3001);
httpServer.listen(port, () => {
console.log(`Listening on port ${port}...`);
});
Expand Down
4 changes: 3 additions & 1 deletion docs/cookbook/limit-repeated-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ In FoalTS you can implement a rate limiter like the [express-rate-limit](https:/
> Note: Because the rate limiter response for rate limited requests does not get handled by FoalTS and its hooks, you need to manually set the default FoalTS headers to the response object of the rate limiter in its `handle` function.
> If you don't manually set any headers only the default Express.js headers will be set in the response.

> warning: version 2

*src/index.ts*
```typescript
// std
Expand Down Expand Up @@ -45,7 +47,7 @@ async function main() {
const app = createApp(AppController, expressApp);

const httpServer = http.createServer(app);
const port = Config.get('port', 3001);
const port = Config.get('port', 'number', 3001);
httpServer.listen(port, () => {
console.log(`Listening on port ${port}...`);
});
Expand Down
10 changes: 5 additions & 5 deletions docs/databases/mongodb.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ const { Config } = require('@foal/core');

module.exports = {
type: "mongodb",
database: Config.get2('database.database', 'string'),
dropSchema: Config.get2('database.dropSchema', 'boolean', false),
database: Config.get('database.database', 'string'),
dropSchema: Config.get('database.dropSchema', 'boolean', false),
entities: ["build/app/**/*.entity.js"],
host: Config.get2('database.host', 'string'),
port: Config.get2('database.port', 'number'),
synchronize: Config.get2('database.synchronize', 'boolean', false)
host: Config.get('database.host', 'string'),
port: Config.get('database.port', 'number'),
synchronize: Config.get('database.synchronize', 'boolean', false)
}

```
Expand Down
24 changes: 14 additions & 10 deletions docs/databases/typeorm.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,22 @@ TypeORM is integrated by default in each new FoalTS project. This allows you to

When creating a new project, an `SQLite` database is used by default as it does not require any additional installation (the data is saved in a file). The connection configuration is stored in `ormconfig.js` and `default.json` located respectively at the root of your project and in the `config/` directory.

> warning: version 2

*ormconfig.js*
```js
const { Config } = require('@foal/core');

module.exports = {
type: 'sqlite',
database: Config.get('database.database'),
dropSchema: Config.get('database.dropSchema', false),
database: Config.get('database.database', 'string'),
dropSchema: Config.get('database.dropSchema', 'boolean', false),
entities: ['build/app/**/*.entity.js'],
migrations: ['build/migrations/*.js'],
cli: {
migrationsDir: 'src/migrations'
},
synchronize: Config.get('database.synchronize', false)
synchronize: Config.get('database.synchronize', 'boolean', false)
}
```

Expand Down Expand Up @@ -91,21 +93,23 @@ Two packages are required to use TypeORM with FoalTS:

This section shows how to configure **MySQL** or **PostgreSQL** with Foal.

> warning: version 2

*ormconfig.js*
```js
const { Config } = require('@foal/core');

module.exports = {
type: 'mysql', // or 'postgres'

host: Config.get('database.host'),
port: Config.get('database.port'),
username: Config.get('database.username'),
password: Config.get('database.password'),
database: Config.get('database.database'),
host: Config.get('database.host', 'string'),
port: Config.get('database.port', 'number'),
username: Config.get('database.username', 'string'),
password: Config.get('database.password', 'string'),
database: Config.get('database.database', 'string'),

dropSchema: Config.get('database.dropSchema', false),
synchronize: Config.get('database.synchronize', false),
dropSchema: Config.get('database.dropSchema', 'boolean', false),
synchronize: Config.get('database.synchronize', 'boolean', false),

entities: ["build/app/**/*.entity.js"],
migrations: ["build/migrations/*.js"],
Expand Down
Loading