Typescript Dependency Contracts for dependency inversion
npm install @jonloucks/contracts-ts
import { CONTRACTS, createContract } from '@jonloucks/contracts-ts';
import {
AutoClose,
bind,
claim,
Contract,
createExtractor,
createLifeCycle,
createRepository,
createSingleton,
createValue,
createContract,
enforce,
guardFunctions,
isBound
} from "@jonloucks/contracts-ts/api/Convenience";
// Define a service interface
interface Logger {
log(message: string): void;
}
// Create a contract for the service
const LOGGER_CONTRACT: Contract<Logger> = createContract<Logger>({
name: "Logger",
test: (obj: unknown): obj is Logger => { // example of duck-typing check
return guardFunctions(obj, 'log'); // example of using guardFunctions helper
}
});
bind<Logger>(LOGGER_CONTRACT,
createSingleton<Logger>(
() => ({
log: (message: string) => {
console.log("LOG:", message);
}
})));
const logger : Logger = enforce<Logger>(LOGGER_CONTRACT);
logger.log("Using the service in the test.");
npm install
npm run build
npm test
npm run test:watch
npm run test:coverage
npm run lint
npm run lint:fix
npm run docs
npm run badges
contracts-ts
├── .github
│ ├── ISSUE_TEMPLATE
│ │ ├── bug_report.md
│ │ └── feature_request.md
│ └── workflows
│ ├── main-pull-request-matrix.yml
│ ├── main-pull-request.yml
│ ├── main-push.yml
│ └── main-release.yml
├── CODE_OF_CONDUCT.md
├── CODING_STANDARDS.md
├── CONTRIBUTING.md
├── editorconfig
├── eslint.config.mjs
├── jest.config.js
├── LICENSE
├── package-lock.json
├── package.json
├── PULL_REQUEST_TEMPLATE.md
├── README.md
├── scripts
│ ├── badge-template.svg.dat
│ └── tsconfig.json
├── SECURITY.md
├── src
│ ├── index.ts
│ ├── version.ts
│ ├── api
│ │ ├── *.ts
│ │ ├── *.api.ts
│ ├── auxiliary
│ │ ├── *.ts
│ │ ├── *.impl.ts
│ │ ├── *.test.ts // internal implementation specific
│ │ └── *.api.ts
│ ├── impl
│ │ ├── *.ts
│ │ ├── *.impl.ts
│ │ ├── *.test.ts // internal implementation specific
│ │ └── *.api.ts
│ ├── test
│ │ └── *.test.ts
│ └── never-publish // non shippable development scripts
│ ├── *.ts
│ ├── *.*. // data files etc
│ └── *.test.ts
├── tsconfig.json
└── typedoc.json
The CI workflow runs on every push and pull request to main branch. It:
The GitHub publishings workflows are run to make an official release.
To set up your own publishing:
npm pkg delete private to remove the private flag from the package.name field in package.json to your desired package name.MIT