{"_id":"@8pattern/jmiddleware","_rev":"3-a1c82b7c55ac1d694c2970dc15bd1276","name":"@8pattern/jmiddleware","dist-tags":{"latest":"0.0.3"},"versions":{"0.0.1":{"name":"@8pattern/jmiddleware","version":"0.0.1","description":"Organize your functions just like Koa or Express","main":"index.js","scripts":{"dev":"rollup -c -w --environment NODE_ENV:development","build":"rollup -c --environment NODE_ENV:production","test":"jest","lint":"tslint src/**/*.{ts,tsx} -t verbose","test:watch":"jest --watchAll","test:coverage":"jest --coverage"},"repository":{"type":"git","url":"git+https://github.com/8pattern/jMiddleware.git"},"keywords":["middleware"],"devDependencies":{"@babel/preset-typescript":"^7.6.0","@rollup/plugin-babel":"^5.3.0","@types/jest":"^26.0.22","babel-loader":"^8.0.6","jest":"^27.0.4","jest-cli":"^27.0.4","rollup":"^2.44.0","rollup-plugin-terser":"^7.0.2","rollup-plugin-typescript2":"^0.30.0","ts-jest":"^27.0.3","tslint":"^6.1.3","typescript":"^4.3.2"},"author":{"name":"8pattern"},"license":"MIT","bugs":{"url":"https://github.com/8pattern/jMiddleware/issues"},"homepage":"https://github.com/8pattern/jMiddleware#readme","gitHead":"1916a70e8e48dbdc6bdcefd6fd0a6b62b3b0e18f","_id":"@8pattern/jmiddleware@0.0.1","_nodeVersion":"14.17.0","_npmVersion":"6.14.13","dist":{"integrity":"sha512-PT8Ms/IyZ89MZRXv8J0TyAFYBGfvG43IGrzFd7HWh/E6fg4zlMjRtLpiztWnULCSyrTDWayDLSjHSHZo28Xdtw==","shasum":"30d0666f421e1e028e31d175a792bc604c9c27e3","tarball":"https://registry.npmjs.org/@8pattern/jmiddleware/-/jmiddleware-0.0.1.tgz","fileCount":15,"unpackedSize":15959,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgxvejCRA9TVsSAnZWagAA8jAP/jG9vjyL01ZxuQfuj66n\n/ff4EWE/RbXbvKV10YfQsP5eS6QwBPRBkmrzYC8TuJOBKksKv1r8AC34PmSa\nGt/l6b7Ya+IlHXer4ydzIbvwpzIanwjL2VQ8Z+FVEITGaRRthN88LIXg/itA\ny+QCH4NWaMs8lpJ9y2sIMtqYzcfAL9AZP5FHccrOkwdUI5lhSS0tBA2y60Ku\nWah1qoPYsXYHl0UQGOWLQYX4I4R19KcWvionsRZf1Vx2s45H9YZPY5sa5ux1\nuBfXdGAKNOZRG3cavry1+JCiBtDmUdB8s4AmKEBe4tHhpgBWIYRkuq92GyWM\nh/HnlZ0+iWsEq12OLREMRyW09aOc15GtY0Ssr3BzHZKk3L+kp4nXmxra3JUz\ngFRsyy+DF1wOn9+dLI8L0kHFgdD05NH36t3/ZJKhlM54EhWPCItnpAXrToDY\n3T0aThrILt+qcafRnXtqD4EPXaHc706X+3qabAsXrut0g/vBrksAsujxHaeg\nWS7P9IHgkWLhKtlm8RMy4VKHm5VQKCTiCRCgNFvHrJ9NZiOvvJJz5LVzHLrR\njgF/qv38JKLSvCMXfhR+euSe1Es0L9lrE4Xdk0mgS0oNNO/qOwhBK8xTOrTi\neJAeWF0RPaWxxlvPYD8+w4RLikpot7JcGTkJ697YRtOstoM/NOYNWVsqw73V\nZn56\r\n=rqOI\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC6LUuHUioZJE+b2bukQs28C6gB/CaeKOxuIPtMnI6dfgIgV5iWsdNnz53pqz01/s6SJPBB62J6NBPVREf2nwD20Lk="}]},"_npmUser":{"name":"8pattern","email":"eighth.principle@gmail.com"},"directories":{},"maintainers":[{"name":"8pattern","email":"eighth.principle@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/jmiddleware_0.0.1_1623652258924_0.32064959211001764"},"_hasShrinkwrap":false},"0.0.2":{"name":"@8pattern/jmiddleware","version":"0.0.2","description":"Organize your functions just like Koa or Express","main":"index.js","scripts":{"dev":"rollup -c -w --environment NODE_ENV:development","build":"rollup -c --environment NODE_ENV:production","test":"jest","lint":"tslint src/**/*.{ts,tsx} -t verbose","test:watch":"jest --watchAll","test:coverage":"jest --coverage"},"repository":{"type":"git","url":"git+https://github.com/8pattern/jMiddleware.git"},"keywords":["middleware"],"devDependencies":{"@babel/preset-typescript":"^7.6.0","@rollup/plugin-babel":"^5.3.0","@types/jest":"^26.0.22","babel-loader":"^8.0.6","jest":"^27.0.4","jest-cli":"^27.0.4","rollup":"^2.44.0","rollup-plugin-terser":"^7.0.2","rollup-plugin-typescript2":"^0.30.0","ts-jest":"^27.0.3","tslint":"^6.1.3","typescript":"^4.3.2"},"author":{"name":"8pattern"},"license":"MIT","bugs":{"url":"https://github.com/8pattern/jMiddleware/issues"},"homepage":"https://github.com/8pattern/jMiddleware#readme","gitHead":"9812f5714fa151c9fb4341fc0c40fa47d579a9df","_id":"@8pattern/jmiddleware@0.0.2","_nodeVersion":"14.17.0","_npmVersion":"6.14.13","dist":{"integrity":"sha512-B87S9jJmPqd1ObDxCJ3eN0m1IeG8p4CKq+fHB1t0OXa1odlIhDfr1FDf+fCDlAf4RgWGNBx7NBI1TTHkh3qkzA==","shasum":"dd2e39ebc9002bff42290d20075d8fad276c0386","tarball":"https://registry.npmjs.org/@8pattern/jmiddleware/-/jmiddleware-0.0.2.tgz","fileCount":15,"unpackedSize":22726,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgzWizCRA9TVsSAnZWagAAZhEP/1RROQBR+lmjLvwrmmP1\ni34GR6u/wXHNH5SGMylw7rqTwAFp77AcHo+l3F7NcPcy3kzQ3sFKGrIONAkY\ngkpmq0DtIDCMeBoLW66vIdLMwovTRUcpLaM5Mbk4byBKo5AepPv8H2xLAMe5\nbsDd7naV3p4ZcQw1mM8Dm20KfkTGEPFV7fkfinQQBffJD0HiEY9FItFWh/AA\n52swU74NHZh/8A4JP0tiDUFIpkExjP5+b6tnZTHtmxopqyAdeqJl/y/n88yg\nuN5W9FSJ46zEC0OZfRQVFRQpJyYd02M9CV/SpIfL7NmrR4bqP+FCFL2ArzNH\n0VPif/+M2rB3DrVBYa0ft+SDrKUOIMqASLfpagyglvXFI+wbdDBiu7vfCmbA\nff/uwBmI8TGsUO1VX59Hd7KuVuBbvymwKL0WAXCBWFZoMCiayV03QOPAtB59\nEb9bH90LvZq03voykMweGn22No/NWiogu0dHvmA4T9n8NSkwCWsk0O61wiy/\nYmWEaSevlPrfONN9D6o3T1fC/ixl8TUOFG0MQMUrn5v2xQXsEzjm1SJvqfBK\n8DqJtmUXNDVqm8SbNm49/Q4YtebFI0HS4854HLabrcBXiUlIJdwPGplVxfEL\nozlPETLtXFfpDulVH7+gXhGGSSkkTWcR2LOPVS5oPH6SaJi5HedXQ2ZTvhc3\nHe86\r\n=YogJ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCICh1N5KXSTov0UtWGD+IrPKGK2VUANt1wcNjcojbzzThAiEA1Kb/zfE6ajkBql64hKr3j5vFf2dGjaPxdbFGBMbp0pU="}]},"_npmUser":{"name":"8pattern","email":"eighth.principle@gmail.com"},"directories":{},"maintainers":[{"name":"8pattern","email":"eighth.principle@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/jmiddleware_0.0.2_1624074418816_0.4838819070962248"},"_hasShrinkwrap":false},"0.0.3":{"name":"@8pattern/jmiddleware","version":"0.0.3","description":"Organize your functions just like Koa or Express","main":"dist/jMiddleware.js","types":"src","scripts":{"dev":"rollup -c -w --environment NODE_ENV:development","build":"rollup -c --environment NODE_ENV:production","test":"jest","lint":"eslint src","test:watch":"jest --watchAll","test:coverage":"jest --coverage"},"repository":{"type":"git","url":"git+https://github.com/8pattern/jMiddleware.git"},"keywords":["middleware"],"devDependencies":{"@babel/preset-typescript":"^7.6.0","@rollup/plugin-babel":"^5.3.0","@types/jest":"^26.0.22","@typescript-eslint/eslint-plugin":"^4.27.0","@typescript-eslint/parser":"^4.27.0","babel-loader":"^8.0.6","eslint":"^7.29.0","eslint-plugin-jest":"^24.3.6","jest":"^27.0.4","jest-cli":"^27.0.4","rollup":"^2.44.0","rollup-plugin-terser":"^7.0.2","rollup-plugin-typescript2":"^0.30.0","ts-jest":"^27.0.3","tslint":"^6.1.3","typescript":"^4.3.2"},"author":{"name":"8pattern"},"license":"MIT","bugs":{"url":"https://github.com/8pattern/jMiddleware/issues"},"homepage":"https://github.com/8pattern/jMiddleware#readme","gitHead":"d5b276cd2b4cfd272c434e5a3bf8a14bed304775","_id":"@8pattern/jmiddleware@0.0.3","_nodeVersion":"14.17.0","_npmVersion":"6.14.13","dist":{"integrity":"sha512-bjzg07s8I7UTbB56vS8f4l0XzrZ/KLmy1b01LB69x9S5nNeUVH8r4ZOsnBW4T5+QYY4H2gI4Ix71SC3uWSpq7w==","shasum":"8998c13a8daf10220186548576c07cab0f6e97a8","tarball":"https://registry.npmjs.org/@8pattern/jmiddleware/-/jmiddleware-0.0.3.tgz","fileCount":15,"unpackedSize":22977,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg196yCRA9TVsSAnZWagAA9dkP/iTtHPisbEwrMCIOliab\nnKIXvXSSdU2c9ip+qWsJ93gI6xI+YriWADvciWKF1r57zMls9aODeavb8oZu\nPmmtcbR/KYqqEXqXIewygEJYBv7iyeFOtoO0uYYv9sbqh/4wavYryCb4YY9y\nWoRb6paMoKpNwxbGVXqrq8hZ/tYU52hHyx8+d9E3/B9HjJOeAVRxk3rNPwDb\n2Fm4WEJqnpbiVygaYEpR9Tpv0XmOWsDLP678WLJC/xATkftURULWVKcLU3/+\nEpJ+ITcWQZIv5E3cfmExVMp2yeCAIXH0W6kRKRx0aXNNeFmxu2PnEDk7Ru3f\n+j7/ysYqLnCRnyz485KynsscCNCK6O2bEpFXK1NppY0P6f+Ot2tLNpSByn2k\nPLVzCILpdMYCDDoGiOcZODlfJ7yPXEB1sJl9Bnuin330FDCzD0+B08koPoZ0\nXJ2wwkBvcJB3TE/FMrAxcy1B1A8k2+cHNHZhgl98zKQusbNiV2mvJUCC27P4\nbbCANJgSXnh6C1AAuYL/4UM9WrVlGsatpFHTCY54RFPvGlmoJDYmR2QYKuF/\no4QlpMqyR3dwaLqC5c8mygv8Srn1FS9QyEan59sbTmMKPtXwVVVJzFoHQaz5\nqBl3AyB6J0TxjdeOj0i6HMUR9FOph6QvPnb/dMG46JfEWtEHIZFOFG19c4As\nPqBj\r\n=VeKm\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHEhagt2zdQu/RcbNFV+2O8FbeMAjqPXN960qHuQ5xhpAiBL3rnC4hx0yZ+HuAJ3SeDSc5dlERyqtMbSnRNP6lfEZQ=="}]},"_npmUser":{"name":"8pattern","email":"eighth.principle@gmail.com"},"directories":{},"maintainers":[{"name":"8pattern","email":"eighth.principle@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/jmiddleware_0.0.3_1624759986374_0.9643962432541879"},"_hasShrinkwrap":false}},"time":{"created":"2021-06-14T06:30:58.883Z","0.0.1":"2021-06-14T06:30:59.065Z","modified":"2022-04-04T10:50:08.213Z","0.0.2":"2021-06-19T03:46:58.922Z","0.0.3":"2021-06-27T02:13:06.508Z"},"maintainers":[{"name":"8pattern","email":"eighth.principle@gmail.com"}],"description":"Organize your functions just like Koa or Express","homepage":"https://github.com/8pattern/jMiddleware#readme","keywords":["middleware"],"repository":{"type":"git","url":"git+https://github.com/8pattern/jMiddleware.git"},"author":{"name":"8pattern"},"bugs":{"url":"https://github.com/8pattern/jMiddleware/issues"},"license":"MIT","readme":"# jMiddleware\n![Platform: Node/Web](https://img.shields.io/static/v1?label=Platform&message=Node/Web&color=47b8e0)\n\n![](https://github.com/8pattern/jMiddleware/actions/workflows/Check.yml/badge.svg)\n![](https://github.com/8pattern/jMiddleware/actions/workflows/Publish.yml/badge.svg)\n\nOrganize your functions just like Koa or Express\n\n> If you are familar with the middleware framework of Koa or Express, [**Differences From Koa**](differences-from-koa) may be more suitable to acquaint the package.\n\n## Hello, jMiddleware\n```typescript\n  import JMiddleware from '@8pattern/jmiddleware'\n  const jMiddleware = new JMiddleware();\n  jMiddleware.use(async (_, next) => {\n    const start = Date.now();\n    await next();\n    console.log(Date.now() - start);\n  });\n  jMiddleware.startAsync();\n```\n\n## Usage\n+ Install\n```\n  npm i @8pattern/jmiddleware\n```\n+ Import\n```javascript\n  import JMiddleware from '@8pattern/jmiddleware'\n```\n+ Instantiate\n```javascript\n  const jMiddleware = new JMiddleware();\n```\n+ Register middlewares\n```javascript\n  jMiddleware.use((_, next) => {\n    const start = Date.now();\n    next();\n    console.log(Date.now() - start);\n  });\n```\n+ Start\n```javascript\n  jMiddleware.startSync();\n```\n\n## APIs\n### Constructor\n#### Context\n> Type: **any**\n\n> Default: **undefined**\n\nA variable with any types can be defined when getting the instance, which can't be re-asigned in middlewares.\n```javascript\n  const Context = {};\n  const jMiddleware = new JMiddleware(Context);\n  jMiddleware.use((_, next, ctx) => {\n    console.assert(ctx === Context);\n    next();\n  });\n  jMiddleware.startSync();\n```\n\nIt is not necessary.\n```javascript\n  const jMiddleware = new JMiddleware();\n  jMiddleware.use((_, next, ctx) => {\n    console.assert(ctx === undefined);\n    next();\n  });\n  jMiddleware.startSync();\n```\n\n### Middleware\nMiddlewares will receive three parameters and also can return a value.\n#### Parameter: value\n> Type: **unknown**\n\n> Default: **last asigned value**\n\nIf a middleware want to change the value of the next one, it can tranfer a value to the callback function.\n```typescript\n  jMiddleware.use((_, next) => {\n    next(1);\n  });\n  jMiddleware.use<number>(v => {\n    console.assert(v === 1);\n  });\n  jMiddleware.startSync();\n```\nIf not receiving a value from the previous middleware, the last one will make effect.\n```typescript\n  jMiddleware.use((_, next) => {\n    next(1);\n  });\n  jMiddleware.use((v, next) => {\n    console.assert(v === 1);\n    next(); // not tranfer a value\n  });\n  jMiddleware.use<number>(v => {\n    console.assert(v === 1);\n  });\n  jMiddleware.startSync();\n```\n\n#### Parameter: callback function\n> Type: (val?: any) => unknown\n\n> Default: -\n\nIt will transfer control to the next middleware. It can change the value of the next middleware by the argument and receiving the return from the next middleware.\n\n```typescript\n  jMiddleware.use((_, next) => {\n    const res = next(1);\n    console.assert(res === 2)\n  });\n  jMiddleware.use<number>(v => {\n    console.assert(v === 1);\n    return 2;\n  });\n  jMiddleware.startSync();\n```\n\n#### Parameter: context\n> Type: **any**\n\n> Default: **undefined**\n\nIt is defined when instantiation.\n```javascript\n  const Context = {};\n  const jMiddleware = new JMiddleware(Context);\n  jMiddleware.use((_, next, ctx) => {\n    console.assert(ctx === context);\n    next();\n  });\n  jMiddleware.startSync();\n```\n\n#### Return\n> Type: **any**\n\n> Default: **undefined**\n\nAny middlewares are able to return a value to the previous one (for the first middleware, its return value will be regarded as the final result).\n\n```javascript\n  jMiddleware.use((v, next) => {\n    const val = `1-before ${v}`;\n    const res = next(val);\n    return `${res} 1-after`;\n  });\n  jMiddleware.use((v, next) => {\n    const val = `2-before ${v}`;\n    const res = next(val);\n    return `${res} 2-after`;\n  });\n  const res = jMiddleware.startSync('0');\n  console.log(res); // 2-before 1-before 0 2-after 1-after\n```\n\n### Entry\nWhen defining the middlewares, the package provides two methods to start the process: `startSync` and `startAsync`. The only difference is the `startAsync` regards all middlewares as asynchronous methods but `startSync` regards middlewares as synchronous methods.\n```javascript\n  jMiddleware.use((_, next) => next());\n  console.assert(jMiddleware.startSync() === undefined);\n  console.assert(jMiddleware.startAsync() instanceof Promise);\n```\n\n## Differences From Koa\nThe package is inspired by [Koa](https://koajs.com/), but it isn't designed for web applications, which accounts for some diferences.\n\n### Parameters\nIn Koa, middlewares receive two parameters: a context and a callback function. And the context parameter is a complex object to fit web applications. But in this package, middlewares have three parameters.\n\nThe first parameter is a variable can be asigned with any types.\n```javascript\n  // Koa\n  app.use(ctx => {\n    ctx.body = 'Hello Koa';\n  });\n\n  // jMiddleware\n  jMiddleware.use(val => {\n    console.log(val); // 'any value'\n  });\n  jMiddleware.startAsync('any value');\n```\n\nThe second parameter is a callback function which can transfer the value to the next middleware.\n```javascript\n  // Koa\n  app.use(async (ctx, next) => {\n    await next();\n  });\n\n  // jMiddleware\n  jMiddleware.use(async (val, next) => {\n    await next('any value from prev middleware')\n  });\n  jMiddleware.use(val => {\n    console.log(val); // 'any value from prev middleware'\n  });\n  jMiddleware.startAsync();\n```\nSince the package aims to organize functions in a way of middlewares, we make its parameters more flexiable. If you are familar with the way of Koa, you can asign a object as the initial value.\n```javascript\n  // jMiddleware\n  jMiddleware.use(val => {\n    val.body = 'Hello';\n  });\n  jMiddleware.startAsync({});\n```\n\nThe third parameter is a varaible which only can be assigned when instantiation.\n```javascript\n  // jMiddleware\n  const jMiddleware = new JMiddleware('any value');\n  jMiddleware.use((_, next, ctx) => {\n    console.log(ctx); // any value;\n  });\n  jMiddleware.startAsync();\n```\n\n### Async or Sync\nIn Koa，middlewares are designed as asynchronous methods. But in the package, asynchronous and synchronous methods are both allowed. Besides some scenarios where synchronous methods are more suitable, `async / await` keywords can be avoided when using synchronous methods. The switch are how you start the middlewares.\n```javascript\n  // Async\n  jMiddleware.use(async (_, next) => {\n    await next();\n  }); \n  jMiddleware.startAsync();\n\n  // Sync\n  jMiddleware.use((_, next) => {\n    next();\n  }); \n  jMiddleware.startSync();\n```\n\n### Return\nKoa doesn't need return values, which are allowed in this package. The rule is any middleware will return a value to its previous middleware (for the first middleware, its return value will be regarded as the final result).\n```javascript\n  jMiddleware.use((v, next) => {\n    const val = `1-before ${v}`;\n    const res = next(val);\n    return `${res} 1-after`;\n  });\n  jMiddleware.use((v, next) => {\n    const val = `2-before ${v}`;\n    const res = next(val);\n    return `${res} 2-after`;\n  });\n  const res = jMiddleware.startSync('0');\n  console.log(res); // 2-before 1-before 0 2-after 1-after\n```","readmeFilename":"README.md"}