Skip to content

Commit

Permalink
build(mwcp): breaking change ESM only
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Sep 4, 2023
1 parent a887dbc commit f747789
Show file tree
Hide file tree
Showing 32 changed files with 387 additions and 239 deletions.
6 changes: 2 additions & 4 deletions packages/midway-component-fetch/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
// process.env.MIDWAY_SERVER_ENV = 'unittest'
// process.env['NODE_ENV'] = 'unittest'
const { Bootstrap } = require('@midwayjs/bootstrap');
Bootstrap.run();
import { Bootstrap } from '@midwayjs/bootstrap';
await Bootstrap.run();
44 changes: 28 additions & 16 deletions packages/midway-component-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@
"http",
"ts"
],
"main": "dist/index.js",
"type": "module",
"bin": {},
"types": "dist/index.d.ts",
"imports": {
"#package.json": "./package.json",
"##/*": "./dist/*",
"#@/*": "./test/*"
},
"exports": {
".": "./dist/index.js",
"./package.json": "./package.json"
},
"bugs": {
"url": "https://github.com/waitingsong/fetch/issues"
},
Expand All @@ -32,14 +42,18 @@
"@mwcp/share": "6 - 8"
},
"engines": {
"node": ">=18.12.0"
"node": ">=18.12.0",
"@midwayjs/core": ">=3.12.0"
},
"files": [
"README.*.md",
"Dockerfile",
"npm-shrinkwrap.json",
"tsconfig.json",
"tsconfig.base.json",
"bin/*.js",
"asset",
"bin",
"database",
"dist",
"!**/*.spec.*",
"!**/*.tsbuildinfo"
Expand All @@ -49,23 +63,21 @@
},
"scripts": {
"build": "npm run tsc && tsc-alias",
"clean": "npm run clean:lock && npm run clean:cache && npm run clean:dist && npm run clean:log",
"clean:cache": "rm -rf .eslintcache .tsbuildinfo .vscode/.jest-cache",
"clean:dist": "rm -rf dist/* .tsbuildinfo",
"clean:lock": "rm package-lock.json -f",
"clean": "npm run clean:lock && npm run clean:dist && npm run clean:log",
"clean:cache": "rm -rf .eslintcache .tsbuildinfo",
"clean:dist": "npm run clean:cache && rm -rf dist/*",
"clean:lock": "rm -f package-lock.json",
"clean:log": "rm -rf ./logs ./run",
"cov": "cross-env MIDWAY_SERVER_ENV=unittest TS_NODE_PROJECT=test/tsconfig.json c8 mocha --parallel=false",
"cov": "c8 --all npm run test -- --parallel=false",
"db:gen": "kmore gen --project tsconfig.cjs.json --path ./src",
"db:gen-cjs": "kmore gen --project tsconfig.cjs.json --path ./src --format cjs",
"debug": "cross-env MIDWAY_SERVER_ENV=local ../../node_modules/.bin/midway-bin dev -ts --fast --debug",
"dev": "cross-env MIDWAY_SERVER_ENV=local ../../node_modules/.bin/midway-bin dev --ts --fast --port=7001",
"dev:debug": "cross-env MIDWAY_SERVER_ENV=local NODE_DEBUG=midway* ../../node_modules/.bin/midway-bin dev --ts",
"dev": "cross-env MIDWAY_SERVER_ENV=local mwtsc --watch --run @midwayjs/mock/app",
"lint": "eslint --fix --cache {src,test}/**/*.ts",
"lint:nofix": "eslint --cache {src,test}/**/*.ts",
"purge": "npm run clean && rm node_modules -rf && rm package-lock.json -f",
"start": "node bootstrap.js",
"test": "cross-env MIDWAY_SERVER_ENV=unittest TS_NODE_PROJECT=test/tsconfig.json mocha ",
"test:local": "cross-env MIDWAY_SERVER_ENV=unittest TS_NODE_PROJECT=test/tsconfig.json ../../node_modules/.bin/mocha ",
"predev": "npm run build",
"pretest": "npm run build",
"rp": "rollup -c rollup.config.js --context this",
"start": "cross-env NODE_ENV=production node ./bootstrap.js",
"test": "cross-env MIDWAY_SERVER_ENV=unittest TS_NODE_PROJECT=test/tsconfig.json mocha --parallel=false",
"tsc": "tsc -b"
}
}
28 changes: 28 additions & 0 deletions packages/midway-component-fetch/src/app/default.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
Config as _Config,
Controller,
Get,
} from '@midwayjs/core'

import { Config, ConfigKey, Msg } from '##/lib/types.js'


@Controller(`/_${ConfigKey.namespace}`)
export class DefaultComponentController {

@_Config(ConfigKey.config) readonly config: Config

@Get('/hello')
hello(): string {
// this.valiateRoute()
return Msg.hello
}

// valiateRoute(): void {
// if (! this.config.enableDefaultRoute) {
// throw new Error('route is not enabled')
// }
// }

}

3 changes: 3 additions & 0 deletions packages/midway-component-fetch/src/app/index.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

export * from './default.controller.js'

4 changes: 2 additions & 2 deletions packages/midway-component-fetch/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Config, MiddlewareConfig } from '../index'
import { Config, MiddlewareConfig } from '../index.js'
import {
initialConfig,
initialMiddlewareConfig,
initMiddlewareOptions,
} from '../lib/config'
} from '../lib/config.js'


export const fetchConfig: Config = {
Expand Down
23 changes: 23 additions & 0 deletions packages/midway-component-fetch/src/config/config.unittest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Config, MiddlewareConfig } from '../index.js'
import {
initialConfig,
initialMiddlewareConfig,
initMiddlewareOptions,
} from '../lib/config.js'


export const fetchConfig: Config = {
...initialConfig,
captureRequestHeaders: [...initialConfig.captureRequestHeaders],
captureResponseHeaders: [...initialConfig.captureResponseHeaders],
enableTrace: true,
}

export const fetchMiddlewareConfig: Readonly<Omit<MiddlewareConfig, 'match'>> = {
...initialMiddlewareConfig,
ignore: [],
options: {
...initMiddlewareOptions,
},
}

46 changes: 30 additions & 16 deletions packages/midway-component-fetch/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
/* eslint-disable @typescript-eslint/no-extraneous-class */
import 'tsconfig-paths/register'

import { join } from 'path'

import { Configuration, ILifeCycle } from '@midwayjs/core'

import { useComponents } from './imports'
import { ConfigKey } from './lib/index'
/* eslint-disable import/max-dependencies */
// import assert from 'node:assert'

import {
Configuration,
ILifeCycle,
ILogger,
Logger,
} from '@midwayjs/core'

import * as DefulatConfig from './config/config.default.js'
// import * as LocalConfig from './config/config.local.js'
import * as UnittestConfig from './config/config.unittest.js'
import { useComponents } from './imports.js'
import {
// Config as Conf,
ConfigKey,
// MiddlewareConfig,
} from './lib/types.js'


@Configuration({
namespace: ConfigKey.namespace,
importConfigs: [join(__dirname, 'config')],
importConfigs: [
{
default: DefulatConfig,
// local: LocalConfig,
unittest: UnittestConfig,
},
],
imports: useComponents,
})
export class AutoConfiguration implements ILifeCycle {

// @App() protected readonly app: Application
@Logger() protected readonly logger: ILogger

// async onReady(): Promise<void> {
// assert(this.app, 'this.app must be set')
// const fetch = await this.app.getApplicationContext().getAsync(FetchComponent)
// assert(fetch)
// }
async onReady(): Promise<void> {

this.logger.info(`[${ConfigKey.componentName}] onReady`)
}
}

4 changes: 3 additions & 1 deletion packages/midway-component-fetch/src/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import * as otel from '@mwcp/otel'

const CI = !! (process.env['MIDWAY_SERVER_ENV'] === 'unittest'
|| process.env['MIDWAY_SERVER_ENV'] === 'local'
|| process.env['NODE_ENV'] === 'unittest'
|| process.env['NODE_ENV'] === 'local'
)

export const useComponents: IComponentInfo[] = [otel]
export const useComponents: IComponentInfo[] = []
if (CI && ! useComponents.includes(koa)) {
useComponents.push(koa)
}
useComponents.push(otel)

export interface IComponentInfo {
Configuration: unknown
Expand Down
7 changes: 4 additions & 3 deletions packages/midway-component-fetch/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
Config,
ConfigKey,
} from './lib/index'
} from './lib/index.js'


export { AutoConfiguration as Configuration } from './configuration'
export * from './lib/index'
export { AutoConfiguration as Configuration } from './configuration.js'
export * from './app/index.controller.js'
export * from './lib/index.js'

export {
FormData,
Expand Down
12 changes: 2 additions & 10 deletions packages/midway-component-fetch/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HeadersKey, AttrNames } from '@mwcp/otel'

import { genRequestHeaders } from './helper'
import { Config, MiddlewareConfig, MiddlewareOptions } from './types'
import { genRequestHeaders } from './helper.js'
import { Config, MiddlewareConfig, MiddlewareOptions } from './types.js'


export const initialConfig: Readonly<Config> = {
Expand Down Expand Up @@ -44,11 +44,3 @@ export const initialMiddlewareConfig: Readonly<Omit<MiddlewareConfig, 'ignore' |
enableMiddleware: true,
}

export enum ConfigKey {
namespace = 'fetch',
config = 'fetchConfig',
middlewareConfig = 'fetchMiddlewareConfig',
componentName = 'fetchComponent',
middlewareName = 'fetchMiddleware'
}

5 changes: 2 additions & 3 deletions packages/midway-component-fetch/src/lib/fetch.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import {
ResponseData,
} from '@waiting/fetch'

import { ConfigKey } from './config'
import { defaultfetchConfigCallbacks } from './helper'
import { Config, FetchOptions } from './types'
import { defaultfetchConfigCallbacks } from './helper.js'
import { Config, ConfigKey, FetchOptions } from './types.js'


@Autoload()
Expand Down
8 changes: 4 additions & 4 deletions packages/midway-component-fetch/src/lib/fetch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import {
} from '@mwcp/otel'
import { Headers, ResponseData, pickUrlStrFromRequestInfo } from '@waiting/fetch'

import type { Context } from '../interface'
import type { Context } from '../interface.js'

import { ConfigKey } from './config'
import { FetchComponent } from './fetch.component'
import { FetchComponent } from './fetch.component.js'
import {
Config,
ConfigKey,
FetchOptions,
} from './types'
} from './types.js'


@Provide()
Expand Down
2 changes: 1 addition & 1 deletion packages/midway-component-fetch/src/lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
retrieveHeadersItem,
} from '@waiting/shared-core'

import { Config, ReqCallbackOptions } from './types'
import { Config, ReqCallbackOptions } from './types.js'


/**
Expand Down
10 changes: 5 additions & 5 deletions packages/midway-component-fetch/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

export * from './config'
export * from './types'
export { FetchComponent } from './fetch.component'
export { FetchService } from './fetch.service'
export * from './config.js'
export * from './types.js'
export { FetchComponent } from './fetch.component.js'
export { FetchService } from './fetch.service.js'
export {
defaultfetchConfigCallbacks,
genRequestHeaders,
} from './helper'
} from './helper.js'

17 changes: 15 additions & 2 deletions packages/midway-component-fetch/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ import type {
Context as TraceContext,
} from '@mwcp/otel'
import type { Headers as UndiciHeaders, Options } from '@waiting/fetch'
import { MiddlewareConfig as MWConfig } from '@waiting/shared-types'
import type { MiddlewareConfig as MWConfig } from '@waiting/shared-types'

import { Context } from '../interface'
import { Context } from '../interface.js'


export enum ConfigKey {
namespace = 'fetch',
config = 'fetchConfig',
middlewareConfig = 'fetchMiddlewareConfig',
componentName = 'fetchComponent',
middlewareName = 'fetchMiddleware'
}

export enum Msg {
hello = 'hello world',
AuthFailed = 'Authentication Failed',
}

// export type FetchOptions = Options
export type FetchOptions = Options & TraceOptions

Expand Down
6 changes: 4 additions & 2 deletions packages/midway-component-fetch/test/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ parserOptions:
rules:
"@typescript-eslint/no-unused-vars": 0
"@typescript-eslint/prefer-ts-expect-error": 0
"node/no-extraneous-import": 0
"unicorn/filename-case": 0
"node/no-unpublished-import": 0

ignorePatterns:
- fixtures
# ignorePatterns:
# - fixtures

16 changes: 0 additions & 16 deletions packages/midway-component-fetch/test/0.dummy.test.ts

This file was deleted.

17 changes: 17 additions & 0 deletions packages/midway-component-fetch/test/00.dummy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import assert from 'node:assert/strict'

import { fileShortPath } from '@waiting/shared-core'

import { testConfig } from './root.config.js'


describe(fileShortPath(import.meta.url), () => {

describe('should work', () => {
it('always passed', () => {
assert(testConfig)
})
})

})

Loading

0 comments on commit f747789

Please sign in to comment.