Various Ts.ED utilities
Find a file
semantic-release-bot 28ac0b459c chore(release): 1.3.2 [skip ci]
## [1.3.2](https://gitlab.jojoxd.nl/jojoxd-npm/tsed/tsed-util/compare/v1.3.1...v1.3.2) (2023-03-26)

### Bug Fixes

* **ci/cd:** Update reviewdog base name as it was changed upstream ([4172d2b](4172d2bdc4))
* **package:** Wrong package specifier for `@tsed/schema` ([c92491a](c92491a87c))
2023-03-26 18:25:05 +00:00
.yarn/releases chore(deps): update yarn to v3.4.1 2023-02-01 17:15:32 +00:00
scripts/mocha feat: initial commit 2023-01-14 17:22:44 +01:00
src chore(linting): Add eslint and reviewdog 2023-02-18 19:06:52 +00:00
test feat(prometheus): Add Prometheus integration 2023-02-11 14:54:28 +01:00
.barrelsby.json feat(prometheus): Add Prometheus integration 2023-02-11 14:54:28 +01:00
.eslintrc.json chore(linting): Add eslint and reviewdog 2023-02-18 19:06:52 +00:00
.gitignore feat: initial commit 2023-01-14 17:22:44 +01:00
.gitlab-ci.yml chore(ci/cd): Update test to run at node19 2023-03-26 20:18:37 +02:00
.mocharc.json feat(scheduler): Add Scheduler module 2023-01-22 16:07:22 +01:00
.npmignore feat: initial commit 2023-01-14 17:22:44 +01:00
.nycrc.json feat: initial commit 2023-01-14 17:22:44 +01:00
.releaserc fix(build): Allow CJS targets to use module sub-paths 2023-01-16 16:55:42 +01:00
.yarnrc.yml chore(deps): update yarn to v3.4.1 2023-02-01 17:15:32 +00:00
CHANGELOG.md chore(release): 1.3.2 [skip ci] 2023-03-26 18:25:05 +00:00
package.json fix(package): Wrong package specifier for @tsed/schema 2023-03-26 17:29:32 +00:00
README.md chore(docs): Fix copy error in readme.md 2023-02-11 15:21:45 +01:00
renovate.json feat: initial commit 2023-01-14 17:22:44 +01:00
tsconfig.json feat: initial commit 2023-01-14 17:22:44 +01:00
tsconfig.serve.json feat(scheduler): Add Scheduler module 2023-01-22 16:07:22 +01:00
tsconfig.spec.json feat: initial commit 2023-01-14 17:22:44 +01:00
tsup.config.ts fix(build): Allow CJS targets to use module sub-paths 2023-01-16 16:55:42 +01:00
yarn.lock chore(yarn): Refresh yarn lockfile 2023-03-26 19:50:18 +02:00

@jojoxd/tsed-util

Multiple small modules to make Ts.ED a bit better.

You can check out the test/integration directory for usages.

Note: Most peer dependencies are marked as optional,
read below to find out what dependencies are needed for which functionality.

MikroORM

yarn install @jojoxd/tsed-util @tsed/mikro-orm @mikro-orm/core

Usage:

import { InjectRepository } from "@jojoxd/tsed-util/mikro-orm";
import { Entity, EntityRepository } from "@mikro-orm/core";

export class MyEntityRepository extends EntityRepository<MyEntity> {}

@Entity({ repository: () => MyEntityRepository, })
export class MyEntity {}

export class MyService
{
    @InjectRepository(MyEntity)
    protected readonly myEntityRepository!: MyEntityRepository;

    // With a specific contextName:
    @InjectRepository(MyEntity, "context")
    protected readonly myContextEntityRepository!: MyEntityRepository;
}

express-session

Note: The ProviderScope should be set to REQUEST on the services you use @InjectSession() in.

// Session.d.ts
import {ParamOptions} from "@tsed/platform-params";
import {Session as ExpressSession} from "express-session";

declare module "@tsed/common"
{
    // As we are re-declaring the interface, we need to re-define the Session decorator
    export function Session(expression: string): ParameterDecorator;
    export function Session(options: Partial<ParamOptions>): ParameterDecorator;
    export function Session(): ParameterDecorator;

    // Your session variables go here
    export interface Session extends ExpressSession
    {
        mySessionVariable?: string;
    }
}
// service.ts
import { Injectable, ProviderScope } from "@tsed/di";

// NOTE: Session is just a re-export of Session in "@tsed/common"
import { InjectSession, Session } from "@jojoxd/tsed-util/express-session";

@Injectable({ scope: ProviderScope.REQUEST, })
export class MyService
{
    @InjectSession()
    protected readonly session?: Session;

    public test()
    {
        const sessionVariable = this.session?.mySessionVariable;
    
        // express-session functions are also available:
        this.session?.save();
    }
}

Redoc

yarn install @jojoxd/tsed-util @tsed/swagger @tsed/schema
// Server.ts
import "@tsed/swagger";
import { defineRedocSettings } from "@jojoxd/tsed-util/redoc";

@Configuration({
    swagger: [
        defineRedocSettings({
            // All your spec options go here, mostly the same as the swagger config
            path: '/api/docs',
        }),
    ],
})
export class Server {}

Scheduler

The scheduler is used to schedule jobs using node-scheduler.

yarn install @jojoxd/tsed-util node-scheduler
// Server.ts
import "@jojoxd/tsed-util/scheduler";
import "./MyScheduler";

@Configuration({
    scheduler: {
        // Maybe only enable in production
        enabled: process.env.NODE_ENV === "production",

        disableScheduleSummary: false,
        logging: true,
    }
})
export class Server {}

// MyScheduler.ts
import { Scheduler, Schedule } from "@jojoxd/tsed-util/scheduler";

@Scheduler({ namespace: "some-namespace", })
class MyScheduler
{
    // This is a Service, so @Inject will work here

    @Schedule({ name: "My Job", rule: "*/5 * * * * *", })
    runMyJob()
    {
        // Do something every 5 seconds
    }
}

Prometheus

Prometheus adds metrics to your app.

yarn install @jojoxd/tsed-util prom-client express-prom-bundle
// Server.ts
import "@jojoxd/tsed-util/prometheus";

@Configuration({
    prometheus: {
        enabled: true,
        path: '/metrics',

        collectDefaultMetrics: true,
        options: { /* Options of express-prom-bundle */ }
    }
})
export class Server {}