From 9dc0ac6abf6f465d9f3163698139f2d566dc878e Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Sun, 31 May 2020 23:15:06 +0200 Subject: [PATCH 1/3] Deno compatibility with documented example --- README.md | 25 ++++++++++++++----------- demo/deno_demo.ts | 27 +++++++++++++++++++++++++++ demo/tsconfig.json | 12 ++++++++++++ deno_dist/index.ts | 1 + dist/index.d.ts | 1 + dist/index.js | 1 + package-lock.json | 41 ++++++++++++++++++++++++++--------------- package.json | 15 +++++++++++---- src/index.ts | 1 + 9 files changed, 94 insertions(+), 30 deletions(-) create mode 100644 demo/deno_demo.ts create mode 100644 demo/tsconfig.json diff --git a/README.md b/README.md index b75fb55..e07173c 100644 --- a/README.md +++ b/README.md @@ -10,24 +10,14 @@ Diosaur is a small dependency injection solution written in Typescript for Deno of code, avoiding obvious bindings and other repetitive stuff. It internally depends on `reflect-metadata` to guess the maximum indications out of your code, but still allows you for finer definition of your services. -## Reflect Metadata -As Diosaur relies on `reflect-metadata` and this library was not officially ported to Deno yet, you'll need -to import it manually in your project. - ## Example ```typescript /** Deno **/ -// Import reflect-metadata -import 'https://raw.githubusercontent.com/rbuckton/reflect-metadata/master/Reflect.js'; - // Import diosaur import { Service, Parameter, Inject, setParameter, getContainer } from 'https://raw.githubusercontent.com/ovesco/diosaur/master/mod.ts'; /** Node **/ -// Import reflect-metadata, first install it `npm install --save reflect-metadata` -import 'reflect-metadata'; - // Import diosaur import { Service, Parameter, Inject } from 'diosaur'; @@ -58,12 +48,25 @@ getContainer().then((container) => { }); ``` -## How does it work Generally speaking, a dependency injection library handles the lifecycle of your services, which means that you don't have to create or remove them, it's handled by the container. In Diosaur, services are Typescript `class` decorated with the `@Service` decorator as illustrated in the upper example. +# Using it in Deno + +Currently use of decorator is not allowed by default in deno. +To run a module requiring diosaur you will need to provide a tsconfig.jsonconfig +file specifying `"experimentalDecorators": true`. + +For example to run the above example in deno: +```bash +git clone https://github.com/ovesco/diosaur +cd diosaur/demo +deno run --config tsconfig.json deno_demo.ts +``` +![image](https://user-images.githubusercontent.com/6702424/83362772-13bf1a80-a394-11ea-8bca-6312539641f8.png) + ### Injecting services Another purpose of dependency injection is actually managing your dependencies for you. You can as such inject other services into your service using the `@Inject` decorator. diff --git a/demo/deno_demo.ts b/demo/deno_demo.ts new file mode 100644 index 0000000..60d1ae9 --- /dev/null +++ b/demo/deno_demo.ts @@ -0,0 +1,27 @@ +import { Service, Parameter, Inject, setParameter, getContainer } from '../mod.ts'; + +@Service() +class Doggo { + constructor(@Parameter('doggoName') private name: string) {} + + bark() { + return this.name.toUpperCase(); + } +} + +@Service() +class JonSnow { + + @Inject() + private doggo: Doggo; + + yell() { + return `I'm Jon with my doggo ${this.doggo.bark()} !`; + } +} + +setParameter('doggoName', 'Ghost'); +getContainer().then((container) => { + const jon = container.get(JonSnow); + console.log(jon.yell()); +}); \ No newline at end of file diff --git a/demo/tsconfig.json b/demo/tsconfig.json new file mode 100644 index 0000000..9677717 --- /dev/null +++ b/demo/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "lib": ["dom", "ES2015"], + "esModuleInterop": true, + "experimentalDecorators": true, + "strictPropertyInitialization": false, + "emitDecoratorMetadata": true, + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true + } +} \ No newline at end of file diff --git a/deno_dist/index.ts b/deno_dist/index.ts index 004ca19..ec3909c 100644 --- a/deno_dist/index.ts +++ b/deno_dist/index.ts @@ -2,6 +2,7 @@ import { IContainer } from "./Container.ts"; import Registrer from "./Metadata/Registrer.ts"; import { Constructor, ServiceClassIdentifier, ServiceIdentifier } from "./Types.ts"; import { FunctionFactory } from "./Factory.ts"; +import "https://raw.githubusercontent.com/rbuckton/reflect-metadata/master/Reflect.js"; import { Service, diff --git a/dist/index.d.ts b/dist/index.d.ts index bb83dad..967dd9c 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,5 +1,6 @@ import { IContainer } from "./Container"; import { Constructor, ServiceClassIdentifier, ServiceIdentifier } from "./Types"; +import "reflect-metadata"; import { Service, Inject, InjectAll, Factory, Parameter } from './Decorators'; export { Service, Inject, InjectAll, Factory, Parameter, IContainer, }; export declare const getContainer: () => Promise; diff --git a/dist/index.js b/dist/index.js index f2fcccd..425e08e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }; import Registrer from "./Metadata/Registrer"; import { FunctionFactory } from "./Factory"; +import "reflect-metadata"; import { Service, Inject, InjectAll, Factory, Parameter, } from './Decorators'; export { Service, Inject, InjectAll, Factory, Parameter, }; export const getContainer = () => __awaiter(void 0, void 0, void 0, function* () { diff --git a/package-lock.json b/package-lock.json index c40102f..0f4a9b3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,8 @@ { - "requires": true, + "name": "diosaur", + "version": "0.0.1", "lockfileVersion": 1, + "requires": true, "dependencies": { "@types/comment-json": { "version": "1.1.1", @@ -17,12 +19,14 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -49,7 +53,8 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -94,7 +99,8 @@ "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true }, "gitignore-parser": { "version": "0.0.2", @@ -106,6 +112,7 @@ "version": "7.1.6", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -125,6 +132,7 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, "requires": { "once": "^1.3.0", "wrappy": "1" @@ -133,7 +141,8 @@ "inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true }, "minimal-polyfills": { "version": "2.1.2", @@ -145,6 +154,7 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -159,6 +169,7 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, "requires": { "wrappy": "1" } @@ -172,7 +183,8 @@ "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true }, "reflect-metadata": { "version": "0.1.13", @@ -186,14 +198,6 @@ "integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=", "dev": true }, - "rimraf": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", - "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==", - "requires": { - "glob": "^7.1.3" - } - }, "run-exclusive": { "version": "2.2.7", "resolved": "https://registry.npmjs.org/run-exclusive/-/run-exclusive-2.2.7.tgz", @@ -209,6 +213,12 @@ "integrity": "sha512-EFzjnGAZ/w2jdX8zxV2annB2yyaW2cih1jCqim0N1PgVGc1+k+QhIuHGpDVnAIcGXUywBk+JON5GQDkKP8QHyQ==", "dev": true }, + "typescript": { + "version": "3.9.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.3.tgz", + "integrity": "sha512-D/wqnB2xzNFIcoBG9FG8cXRDjiqSTbG2wd8DMZeQyJlP1vfTkIxH4GKveWaEBYySKIg+USu+E+EDIR47SqnaMQ==", + "dev": true + }, "url-join": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/url-join/-/url-join-4.0.1.tgz", @@ -218,7 +228,8 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true } } } diff --git a/package.json b/package.json index 3fbf975..90c4887 100644 --- a/package.json +++ b/package.json @@ -2,17 +2,24 @@ "devDependencies": { "@types/node": "^14.0.6", "denoify": "^0.2.18", - "reflect-metadata": "^0.1.13" + "reflect-metadata": "^0.1.13", + "typescript": "^3.9.3" }, "name": "diosaur", "version": "0.0.1", "author": "guillaume hochet", "license": "MIT", - "keywords": ["inversion", "of", "control", "dependency", "injection", "typescript"], + "keywords": [ + "inversion", + "of", + "control", + "dependency", + "injection", + "typescript" + ], "description": "A small dependency injection library in typescript", "scripts": { - "build": "tsc && npm run denoify", - "denoify": "npx denoify" + "build": "tsc && denoify" }, "main": "dist/index.js", "denoPorts": { diff --git a/src/index.ts b/src/index.ts index 239295e..b066517 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import { IContainer } from "./Container"; import Registrer from "./Metadata/Registrer"; import { Constructor, ServiceClassIdentifier, ServiceIdentifier } from "./Types"; import { FunctionFactory } from "./Factory"; +import "reflect-metadata"; import { Service, From 71576f87fa47d3c734a8db5772cbb4ffda8221ec Mon Sep 17 00:00:00 2001 From: Garrone Joseph Date: Mon, 1 Jun 2020 00:10:05 +0200 Subject: [PATCH 2/3] Excluding ./demo/ --- tsconfig.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 261f0db..e28027c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,6 +17,7 @@ "dist", "node_modules", "./deno_dist", - "./mod.ts" + "./mod.ts", + "./demo" ] -} \ No newline at end of file +} From 12120fe311ef70277b06271579bd7bf30e5f390e Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Mon, 1 Jun 2020 02:58:31 +0200 Subject: [PATCH 3/3] Update denoify --- mod.ts | 2 +- package-lock.json | 6 +++--- package.json | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mod.ts b/mod.ts index 5a6a2bb..34a5f6c 100644 --- a/mod.ts +++ b/mod.ts @@ -1,4 +1,4 @@ //Automatically generated by denoify. It is important not to edit this file. -// #{"srcDirPath":"src","denoDistPath":"deno_dist","tsconfigOutDir":"./dist"}# +// #{"srcDirPath":"src","denoDistPath":"deno_dist","tsconfigOutDir":"dist"}# export * from "./deno_dist/index.ts"; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 0f4a9b3..2737f98 100644 --- a/package-lock.json +++ b/package-lock.json @@ -63,9 +63,9 @@ "dev": true }, "denoify": { - "version": "0.2.18", - "resolved": "https://registry.npmjs.org/denoify/-/denoify-0.2.18.tgz", - "integrity": "sha512-oNnJVgSZisN3q4TDs3v1PhPPS2IKNfbIv2uwINijnJxZZ7Uoo5vglx0cGn9z/B9NUbm/nqPvqAAAjdeYbIPhLQ==", + "version": "0.2.21", + "resolved": "https://registry.npmjs.org/denoify/-/denoify-0.2.21.tgz", + "integrity": "sha512-6w99ECFgZPQ9UUKTTozjflAPi5KxU9YIbK4HHtUokY7u/3tz+HvB2epTUNwj+I8sNJO38YqKRQwruIbBU9BnfQ==", "dev": true, "requires": { "@types/comment-json": "^1.1.1", diff --git a/package.json b/package.json index 90c4887..3021a9e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "devDependencies": { "@types/node": "^14.0.6", - "denoify": "^0.2.18", + "denoify": "^0.2.21", "reflect-metadata": "^0.1.13", "typescript": "^3.9.3" },