Skip to content

Commit

Permalink
feat: remove ConverterService usage in the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Apr 2, 2022
1 parent 5d54d0e commit ae4be98
Show file tree
Hide file tree
Showing 21 changed files with 151 additions and 182 deletions.
15 changes: 5 additions & 10 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@ The default configuration is as follows:

```json
{
"rootDir": "path/to/root/project",
"env": "development",
"port": 8080,
"debug": false,
"httpsPort": 8000,
"uploadDir": "${rootDir}/uploads",
"mount": {
"/rest": "${rootDir}/controllers/**/*.ts"
},
"componentsScan": ["${rootDir}/middlewares/**/*.ts", "${rootDir}/services/**/*.ts", "${rootDir}/converters/**/*.ts"]
"uploadDir": "./uploads"
}
```

Expand Down Expand Up @@ -159,11 +154,11 @@ It is also possible to split the configuration by using the @@Module@@:
</Tab>
</Tabs>

### componentsScan
### ~~componentsScan~~ (deprecated)

- type: `string[]`

List of glob pattern to scan directories which contains [Services](/docs/services.md), [Middlewares](/docs/middlewares.md) or [Converters](/docs/converters.md).
List of glob pattern to scan directories which contains [Services](/docs/services.md) or [Middlewares](/docs/middlewares.md).

### middlewares <Badge text="v6.28.0+" />

Expand Down Expand Up @@ -252,7 +247,7 @@ Add providers or modules here. These modules or provider will be built before th
</Tab>
</Tabs>

### exclude
### ~~exclude~~ (deprecated)

- type: `string[]`

Expand Down Expand Up @@ -298,7 +293,7 @@ It's also possible to register resolvers with the @@Module@@ decorator:

### views

Object to configure Views engines with Consolidate. See more on [View engine](/docs/templating.md).
Object to configure Views engines with Ts.ED engines or Consolidate (deprecated). See more on [View engine](/docs/templating.md).

### acceptMimes

Expand Down
6 changes: 3 additions & 3 deletions docs/docs/platform-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import compress from "compression";
import cookieParser from "cookie-parser";
import methodOverride from "method-override";

@ServerSettings({
viewsDir: `${rootDir}/views`
@Configuration({
viewsDir: `${process.cwd()}/views`
})
export class Server extends ServerLoader {
$beforeRoutesInit() {
Expand Down Expand Up @@ -73,7 +73,7 @@ import methodOverride from "method-override";

@Configuration({
views: {
root: `${rootDir}/views`,
root: `${process.cwd()}/views`,
viewEngine: "ejs"
}
})
Expand Down
6 changes: 4 additions & 2 deletions docs/docs/serve-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,20 @@ Since v6.74.0, it's possible to load statics before controllers instead of loadi
Just use the options `hook` to change the default behavior:

```typescript
import * as process from "process";

@Configuration({
statics: {
"/before": [
{
root: `${rootDir}/public`,
root: `${process.cwd()}/public`,
hook: "$beforeRoutesInit"
// ... statics options
}
],
"/after": [
{
root: `${rootDir}/public`,
root: `${process.cwd()}/public`,
hook: "$afterRoutesInit"
// ... statics options
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Module} from "@tsed/di";
import {resolve} from "path";
import {UserController} from "./users/UserController";

@Module({
mount: {
"/rest/v0": [`./controllers/v0/users/*.ts`, `./controllers/v0/groups/*.ts`]
"/rest/v0": [UserController]
}
})
export class ModuleV0 {}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Module} from "@tsed/di";
import {resolve} from "path";
import {UserController} from "./users/UserController";

@Module({
mount: {
"/rest/v1": [`./controllers/v1/**/*.ts`]
"/rest/v1": [UserController]
}
})
export class ModuleV1 {}
15 changes: 12 additions & 3 deletions docs/docs/snippets/configuration/server-endpoint-versionning.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import {Configuration} from "@tsed/di";
import {resolve} from "path";
import * as v1Controllers from "./controllers/v1/index";
import * as v0Controllers from "./controllers/v0/index";

@Configuration({
mount: {
"/rest/v1": [`./controllers/v1/**/*.ts`],
"/rest/v0": [`./controllers/v0/users/*.ts`, `./controllers/v0/groups/*.ts`]
"/rest/v1": [...Object.values(v1Controllers)],
"/rest/v0": [...Object.values(v0Controllers)]
}
})
export class Server {}

// v1/index.ts
export * from "./users/UserControllers";
export * from "./groups/GroupsControllers";

// v0/index.ts
export * from "./users/UserControllers";
export * from "./groups/GroupsControllers";
4 changes: 0 additions & 4 deletions docs/docs/snippets/configuration/server-resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {Configuration} from "@tsed/di";
import {resolve} from "path";
import {myContainer} from "./inversify.config";

const rootDir = resolve(__dirname);

@Configuration({
rootDir,
resolvers: [
{
get(token: any) {
Expand Down
5 changes: 2 additions & 3 deletions docs/docs/snippets/configuration/server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import {Configuration} from "@tsed/di";
import * as Path from "path";
import {MyController} from "./controllers/manual/MyController";

@Configuration({
mount: {
"/rest": [
`./controllers/current/**/*.ts`,
`./controllers/current/**/*.ts`, // deprecated
MyController // support manual import
],
"/rest/v0": [
// versioning
`./controllers/v0/users/*.js`,
`./controllers/v0/users/*.js`, // deprecated
`!./controllers/v0/groups/old/*.ts` // Exclusion
]
}
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/passport.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class CustomUserInfoModel {

@Configuration({
componentsScan: [
`${rootDir}/protocols/*.ts` // scan protocols directory
`${process.cwd()}/protocols/*.ts` // scan protocols directory
],
passport: {
userInfoModel: CustomUserInfoModel
Expand Down
4 changes: 2 additions & 2 deletions packages/orm/adapters-redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"devDependencies": {
"@tsed/core": "6.108.0",
"@types/ioredis": "^4.28.10",
"@types/ioredis": "4.28.10",
"@types/ioredis-mock": "5.6.0",
"ioredis": "4.28.5",
"ioredis-mock": "7.2.0"
Expand All @@ -40,4 +40,4 @@
"ioredis-mock": "^5.2.0",
"uuid": "8.3.2"
}
}
}
3 changes: 2 additions & 1 deletion packages/orm/adapters-redis/src/adapters/RedisAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Adapter, AdapterModel} from "@tsed/adapters";
import {Inject} from "@tsed/di";
import IoRedis, {Redis} from "ioredis";
import IoRedis from "ioredis";
import type {Redis} from "ioredis";
import {v4 as uuid} from "uuid";
import "../services/RedisFactory";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ describe("PlatformResponseFilter", () => {
sandbox.stub(ctx.request, "get").returns("application/json");
sandbox.stub(ctx.request, "accepts").returns(["application/json"]);

// @ts-ignore
platformResponseFilter.types.set("*/*", {
transform(data: unknown, ctx: PlatformContext) {
return {data};
Expand Down Expand Up @@ -162,6 +163,7 @@ describe("PlatformResponseFilter", () => {
const data = {text: "test"};
ctx.endpoint = EndpointMetadata.get(Test, "test");

// @ts-ignore
platformResponseFilter.types.set("*/*", {
transform(data: unknown, ctx: PlatformContext) {
return {data};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import {isSerializable, Type} from "@tsed/core";
import {serialize} from "@tsed/json-mapper";
import {BaseContext, Constant, Inject, Injectable, InjectorService} from "@tsed/di";
import {ResponseFilterKey, ResponseFiltersContainer} from "../domain/ResponseFiltersContainer";
import {ResponseFilterMethods} from "../interfaces/ResponseFilterMethods";
import {ANY_CONTENT_TYPE, getContentType} from "../utils/getContentType";
import {ConverterService} from "./ConverterService";
import {renderView} from "../utils/renderView";

/**
* @platform
*/
@Injectable()
export class PlatformResponseFilter {
types: Map<ResponseFilterKey, ResponseFilterMethods> = new Map();
protected types: Map<ResponseFilterKey, ResponseFilterMethods> = new Map();

@Inject()
injector: InjectorService;
protected injector: InjectorService;

@Constant("responseFilters", [])
protected responseFilters: Type<ResponseFilterMethods>[];

@Constant("converter.additionalProperties")
protected additionalProperties: string;

get contentTypes(): ResponseFilterKey[] {
return [...this.types.keys()];
}
Expand All @@ -44,6 +47,7 @@ export class PlatformResponseFilter {

return contentType;
}

/**
* Call filters to transform data
* @param data
Expand All @@ -65,6 +69,7 @@ export class PlatformResponseFilter {

return data;
}

/**
* Serialize data before calling filters
* @param data
Expand All @@ -77,7 +82,9 @@ export class PlatformResponseFilter {
if (endpoint.view) {
data = await renderView(data, ctx);
} else if (isSerializable(data)) {
data = this.injector.get<ConverterService>(ConverterService)!.serialize(data, {
data = serialize(data, {
useAlias: true,
additionalProperties: this.additionalProperties === "accept",
...endpoint.getResponseOptions(response.statusCode),
endpoint: true
});
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/platform-serverless-http/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ import {TimeslotsController} from "./controllers/TimeslotsController";
}
],
views: {
root: `${rootDir}/views`,
root: "${rootDir}/views",
extensions: {
ejs: "ejs"
}
Expand Down
Loading

0 comments on commit ae4be98

Please sign in to comment.