lrf %!s(int64=2) %!d(string=hai) anos
achega
13afeb2777

+ 11 - 0
.editorconfig

@@ -0,0 +1,11 @@
+# 🎨 editorconfig.org
+
+root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+indent_style = space
+indent_size = 2
+trim_trailing_whitespace = true
+insert_final_newline = true

+ 7 - 0
.eslintrc.json

@@ -0,0 +1,7 @@
+{
+  "extends": "./node_modules/mwts/",
+  "ignorePatterns": ["node_modules", "dist", "test", "jest.config.js", "typings"],
+  "env": {
+    "jest": true
+  }
+}

+ 15 - 0
.gitignore

@@ -0,0 +1,15 @@
+logs/
+npm-debug.log
+yarn-error.log
+node_modules/
+package-lock.json
+yarn.lock
+coverage/
+dist/
+.idea/
+run/
+.DS_Store
+*.sw*
+*.un~
+.tsbuildinfo
+.tsbuildinfo.*

+ 3 - 0
.prettierrc.js

@@ -0,0 +1,3 @@
+module.exports = {
+  ...require('mwts/.prettierrc.json')
+}

+ 10 - 0
index.d.ts

@@ -0,0 +1,10 @@
+export * from './dist/index';
+
+declare module '@midwayjs/core/dist/interface' {
+  interface MidwayConfig {
+    book?: PowerPartial<{
+      a: number;
+      b: string;
+    }>;
+  }
+}

+ 7 - 0
jest.config.js

@@ -0,0 +1,7 @@
+module.exports = {
+  preset: 'ts-jest',
+  testEnvironment: 'node',
+  testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
+  coveragePathIgnorePatterns: ['<rootDir>/test/'],
+  setupFilesAfterEnv: ['./jest.setup.js']
+};

+ 1 - 0
jest.setup.js

@@ -0,0 +1 @@
+jest.setTimeout(30000);

+ 36 - 0
package.json

@@ -0,0 +1,36 @@
+{
+  "name": "free-midway-component",
+  "version": "1.0.9",
+  "description": "",
+  "main": "dist/index.js",
+  "typings": "index.d.ts",
+  "scripts": {
+    "build": "cross-env midway-bin build -c",
+    "test": "cross-env midway-bin test --ts",
+    "cov": "cross-env midway-bin cov --ts",
+    "lint": "mwts check",
+    "lint:fix": "mwts fix"
+  },
+  "keywords": [],
+  "author": "",
+  "files": [
+    "dist/**/*.js",
+    "dist/**/*.d.ts",
+    "index.d.ts"
+  ],
+  "license": "MIT",
+  "devDependencies": {
+    "@midwayjs/cli": "^2.0.0",
+    "@midwayjs/core": "^3.0.0",
+    "@midwayjs/decorator": "^3.0.0",
+    "@midwayjs/koa": "^3.0.0",
+    "@midwayjs/mock": "^3.0.0",
+    "@types/jest": "^29.2.3",
+    "@types/node": "^16.11.22",
+    "cross-env": "^6.0.0",
+    "jest": "^29.2.2",
+    "mwts": "^1.0.5",
+    "ts-jest": "^29.0.3",
+    "typescript": "~4.8.0"
+  }
+}

+ 4 - 0
src/config/config.default.ts

@@ -0,0 +1,4 @@
+export const customKey = {
+  a: 1,
+  b: 'hello',
+};

+ 18 - 0
src/configuration.ts

@@ -0,0 +1,18 @@
+import { Configuration } from '@midwayjs/decorator';
+import * as DefaultConfig from './config/config.default';
+import * as koa from '@midwayjs/koa';
+@Configuration({
+  namespace: 'book',
+  imports: [koa],
+  importConfigs: [
+    {
+      default: DefaultConfig,
+    },
+  ],
+})
+export class BookConfiguration {
+  async onReady() {
+    // TODO something
+    console.log('component book is ready');
+  }
+}

+ 0 - 0
src/controller/BaseController.ts


+ 2 - 0
src/index.ts

@@ -0,0 +1,2 @@
+export { BookConfiguration as Configuration } from './configuration';
+export * from './service/book.service';

+ 19 - 0
src/middleware/book.middleware.ts

@@ -0,0 +1,19 @@
+import { IMiddleware } from '@midwayjs/core';
+import { Middleware } from '@midwayjs/core';
+import { NextFunction, Context } from '@midwayjs/koa';
+@Middleware()
+export default class BookMiddleware
+  implements IMiddleware<Context, NextFunction>
+{
+  resolve() {
+    return async (ctx: Context, next: NextFunction) => {
+      console.log('in book middleware');
+      await next();
+      return 'ok';
+    };
+  }
+
+  static getName() {
+    return 'book_middleware';
+  }
+}

+ 8 - 0
src/service/book.service.ts

@@ -0,0 +1,8 @@
+import { Provide } from '@midwayjs/decorator';
+
+@Provide()
+export class BookService {
+  async getBookById() {
+    return 'hello world book';
+  }
+}

+ 11 - 0
test/index.test.ts

@@ -0,0 +1,11 @@
+import { createLightApp } from '@midwayjs/mock';
+import * as custom from '../src';
+describe('/test/index.test.ts', () => {
+  it('test component', async () => {
+    const app = await createLightApp('', {
+      imports: [custom],
+    });
+    const bookService = await app.getApplicationContext().getAsync(custom.BookService);
+    expect(await bookService.getBookById()).toEqual('hello world');
+  });
+});

+ 21 - 0
tsconfig.json

@@ -0,0 +1,21 @@
+{
+  "compileOnSave": true,
+  "compilerOptions": {
+    "target": "es2018",
+    "module": "commonjs",
+    "moduleResolution": "node",
+    "experimentalDecorators": true,
+    "emitDecoratorMetadata": true,
+    "inlineSourceMap": false,
+    "noImplicitThis": true,
+    "noUnusedLocals": true,
+    "stripInternal": true,
+    "skipLibCheck": true,
+    "noImplicitReturns": false,
+    "pretty": true,
+    "declaration": true,
+    "forceConsistentCasingInFileNames": true,
+    "outDir": "dist"
+  },
+  "exclude": ["dist", "node_modules", "test"]
+}