{"id":22914450,"url":"https://github.com/communitysolidserver/asynchandler","last_synced_at":"2026-02-12T13:08:30.727Z","repository":{"id":255489375,"uuid":"852744086","full_name":"CommunitySolidServer/AsyncHandler","owner":"CommunitySolidServer","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-07T07:14:04.000Z","size":514,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-31T22:41:23.831Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/CommunitySolidServer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2024-09-05T10:52:12.000Z","updated_at":"2025-02-07T07:14:01.000Z","dependencies_parsed_at":"2024-09-05T19:14:24.830Z","dependency_job_id":"ec271098-3a33-468b-b272-9d6e3b147a19","html_url":"https://github.com/CommunitySolidServer/AsyncHandler","commit_stats":null,"previous_names":["communitysolidserver/asynchandler"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommunitySolidServer%2FAsyncHandler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommunitySolidServer%2FAsyncHandler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommunitySolidServer%2FAsyncHandler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommunitySolidServer%2FAsyncHandler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CommunitySolidServer","download_url":"https://codeload.github.com/CommunitySolidServer/AsyncHandler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253749619,"owners_count":21958153,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-12-14T05:14:57.938Z","updated_at":"2026-02-12T13:08:25.707Z","avatar_url":"https://github.com/CommunitySolidServer.png","language":"TypeScript","readme":"# AsyncHandler\n\n[![npm version](https://badge.fury.io/js/asynchronous-handlers.svg)](https://www.npmjs.com/package/asynchronous-handlers)\n\nThis library provides a generic interface which you can use for classes,\nso they can easily be combined using composite patterns.\nAn `AsyncHandler` implementation has 3 functions: `canHandle`, `handle`, and `handleSafe`.\n\n`canHandle` is used to determine if a class supports a given input.\nIn case it does not, it should throw an error.\nIf a `canHandle` call succeeds, the `handle` function can be called to perform the necessary action.\n`handleSafe` is a utility function that combines the above two steps into one.\n\nFor example, assume you want to handle an HTTP request,\nand have a separate class for every HTTP method.\nIn the `canHandle` call for each of those,\nthey would check if the method is the correct one and throw an error if this is not the case.\nIn the `handle` call, they could then perform the necessary action for this method,\nknowing that the request has the correct method.\n\nThis library comes with several utility composite handlers that can be used to combine such handlers into one.\nThese have the same interface, so from the point of view of the caller,\na single HTTP request class, and a composite handler that combines all the HTTP request classes,\nlook and perform the same.\n\n## Composite handlers\n\nBelow is an overview of all composite handlers provided in this library.\n\n### AsyncHandler\n\nThe main interface. Although in practice this is actually an abstract class.\nThis way it can provide a default `canHandle` implementation that always succeeds,\nand a `handleSafe` implementation that calls `canHandle`, and then `handle` if the first succeeds.\n\n### BooleanHandler\n\nTakes as input one or more handlers that return a boolean.\nThis handler will return `true` if any of the input handlers can handle the input and return `true`.\n\n### CachedHandler\n\nCaches the result of the source handler in a [WeakMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap).\nThe input is used as the key to cache with.\nInput field names can also be provided to only use a subset of the input.\n\n### ParallelHandler\n\nRuns the `handleSafe` function of all its source handlers simultaneously.\nThe `canHandle` of this handler will always succeed.\n\n### ProcessHandler\n\nA handler made for worker threads.\nIt can be used to only run the source handler depending on if it is executed on the primary or a worker thread.\n\n### SequenceHandler\n\nRuns all its handlers sequentially and returns the result of the last one that can handle the input.\nThe `canHandle` of this handler will always succeed.\n\n### StaticHandler\n\nA handler that always succeeds and returns the provided value.\n\n### StaticThrowHandler\n\nA handler that always throws the provided error.\nIt can be configured to always succeed `canHandle` and only throw when `handle` is called,\nor to throw in both cases.\n\n### UnionHandler\n\nAn abstract class that combines the results of all its source handlers in some way.\nHow these are combined is determined by the abstract `combine` function.\nIt can be configured to ignore handlers that do not support the input,\nand to ignore errors.\n\n#### ArrayUnionHandler\n\nA `UnionHandler` that combines and flattens the results of its source handlers into a single array.\n\n### WaterfallHandler\n\nThis handler will call the `handle` function of the first source handler where the `canHandle` call succeeds.\n\n## Utility\n\nBesides the composite handlers, some utility functions and classes are provided.\n\n### findHandler\n\nA function that returns the first handler from a list of handlers where `canHandle` succeeds.\nThrows an error if no such handler can be found.\n\n### filterHandlers\n\nFilters a list of handlers to only return those where `canHandle` succeeds.\nThrows an error if this would be an empty list.\n\n### ErrorFactory\n\nAn interface for a factory that can create errors.\nThis can be used in combination with a `StaticThrowHandler`.\n\n#### BaseErrorFactory\n\nAn `ErrorFactory` that creates a standard Error.\n\n#### ClassErrorFactory\n\nAn `ErrorFactory` that reuses the constructor of the input Error.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommunitysolidserver%2Fasynchandler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcommunitysolidserver%2Fasynchandler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommunitysolidserver%2Fasynchandler/lists"}