{"id":16485980,"url":"https://github.com/roziscoding/composer","last_synced_at":"2026-05-01T13:31:19.569Z","repository":{"id":148265469,"uuid":"618192233","full_name":"roziscoding/composer","owner":"roziscoding","description":"Standalone implementation of the \"Chain of Command\" pattern. Heavily inspired by grammY's Composer class","archived":false,"fork":false,"pushed_at":"2023-03-28T15:18:55.000Z","size":46,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-04-26T02:19:32.808Z","etag":null,"topics":["composer","middleware","tasks","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/roziscoding.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-03-24T00:07:10.000Z","updated_at":"2023-07-15T15:29:59.000Z","dependencies_parsed_at":"2023-05-20T07:45:43.985Z","dependency_job_id":null,"html_url":"https://github.com/roziscoding/composer","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/roziscoding/composer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roziscoding%2Fcomposer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roziscoding%2Fcomposer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roziscoding%2Fcomposer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roziscoding%2Fcomposer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/roziscoding","download_url":"https://codeload.github.com/roziscoding/composer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roziscoding%2Fcomposer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32499681,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"online","status_checked_at":"2026-05-01T02:00:05.856Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","middleware","tasks","typescript"],"created_at":"2024-10-11T13:28:00.490Z","updated_at":"2026-05-01T13:31:19.559Z","avatar_url":"https://github.com/roziscoding.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Composer\n\n[![@roziscoding/composer](https://img.shields.io/npm/v/@roziscoding/composer?label=%20\u0026logo=npm\u0026style=flat-square)](https://www.npmjs.com/package/@roziscoding/composer)\n[![deno.land/x/composer](https://shield.deno.dev/x/composer)](https://deno.land/x/composer)\n[![composer](https://github.com/roziscoding/composer/actions/workflows/deno.yml/badge.svg)](https://github.com/roziscoding/composer/actions/workflows/deno.yml)\n\nStandalone implementation of the \"Chain of Command\" pattern. Heavily inspired by\n[grammY's Composer class](https://github.com/grammyjs/grammy/tree/main/src/composer.ts)\n\n## Usage\n\n### Getting started\n\nYou start by creating an instance of the `Composer` class, passing as a type param the type of your context. You then\nregister your middleware functions and call the `execute` function, passing the value of the context. The resulting\npromise will contain the context as returned by the middleware tree.\n\n\u003e Important: you must **always** call `next`. If you don't, your middleware tree will stay stuck and will not finish\n\u003e execution. Also, the call to `next` should always be awaited or returned, so you don't run into concurrency problems.\n\n```typescript\nimport { Composer } from \"https://deno.land/x/composer/mod.ts\";\n\ntype Context = {\n  steps: number;\n};\n\nconst composer = new Composer();\ncomposer.use((ctx, next) =\u003e next({ ...ctx, steps: ctx.steps + 1 }));\n\ncomposer.execute({ steps: 0 }).then(console.log); // { steps: 1 }\n```\n\n### Timeshift Methods\n\nComposer offers two methods, which we call Timeshift Methods, that allow you to register middleware to be run either\n`after` or `before` the regularly registered middleware. This allows you to ensure execution order and make your code\nclearer.\n\n#### Deferring execution\n\nSometimes you need a middleware function to perform tasks after all the other middleware has finished running. To do\nthat, you have two options: you can either `await next(ctx)` and perform your tasks after that; or you can use the\n`after` method, like so:\n\n```typescript\nimport { Composer } from 'https://deno.land/x/composer/mod.ts'\n\ntype Context = {\n  start: number\n  steps: number\n}\n\nconst composer = new Composer\u003cContext\u003e()\ncomposer.after((ctx, next) =\u003e { console.log(`took ${Date.now() - start} ms`}); return next(ctx) })\ncomposer.use((ctx, next) =\u003e next({ ...ctx, start: Date.now() }))\ncomposer.use((ctx, next) =\u003e next({ ...ctx, steps: ctx.steps + 1 }))\n\ncomposer.execute({ steps: 0 }).then(console.log)\n// took xxx ms\n// { steps: 1 }\n```\n\nThe `after` method registers the middleware function in a special way that makes sure that it **always** runs after all\nthe other middleware. As with regular middleware, middleware registered using `after` can modify the context by passing\nthe mutated context, or a mutated clone of it, to the `next` function.\n\n#### Forwarding execution\n\nOpposite to the `after` method, there is the `before` method for when you need to run things before any registered\nmiddleware:\n\n```typescript\nimport { Composer } from 'https://deno.land/x/composer/mod.ts'\n\ntype Context = {\n  start: number\n  steps: number\n}\n\nconst composer = new Composer\u003cContext\u003e()\ncomposer.use((ctx, next) =\u003e next({ ...ctx, start: Date.now() }))\ncomposer.use((ctx, next) =\u003e next({ ...ctx, steps: ctx.steps + 1 }))\ncomposer.use((ctx, next) =\u003e { console.log(`took ${Date.now() - start} ms`}); return next(ctx) })\ncomposer.before((ctx, next) =\u003e next({ ...ctx, start: Date.now() }))\n\ncomposer.execute({ steps: 0 }).then(console.log)\n// took xxx ms\n// { steps: 1 }\n```\n\nThe `before` method works similarly to the `after` method in that it registers the middleware function in a special way\nthat makes sure it will **always** run before any other middleware registered regularly. As with any other middleware,\nyou can also modify the context by passing its new version to `next`, be it a new object or the mutated existing one.\n\n#### Order matters\n\nAs you've seen with the examples, `before` middleware will always run at the beginning of the execution, and `after`\nmiddleware will always run at the end of the execution, no matter if you call them before or after `use`. However, when\nyou call timeshift functions multiple times, they will be executed in the order they were registered. For example:\n\n```typescript\ntype Context = {\n  steps: number[];\n};\n\nconst composer = new Composer\u003cSteps\u003e();\n\ncomposer.before((ctx, next) =\u003e next({ ...ctx, steps: ctx.steps.concat([1]) }));\ncomposer.before((ctx, next) =\u003e next({ ...ctx, steps: ctx.steps.concat([2]) }));\ncomposer.after((ctx, next) =\u003e next({ ...ctx, steps: ctx.steps.concat([3]) }));\ncomposer.after((ctx, next) =\u003e next({ ...ctx, steps: ctx.steps.concat([4]) }));\n\ncomposer.execute({ steps: [] }).then(console.log); // [1, 2, 3, 4]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froziscoding%2Fcomposer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froziscoding%2Fcomposer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froziscoding%2Fcomposer/lists"}