{"id":18582098,"url":"https://github.com/lorefnon/overridable-fn","last_synced_at":"2026-02-04T00:02:48.980Z","repository":{"id":139148576,"uuid":"576711881","full_name":"lorefnon/overridable-fn","owner":"lorefnon","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-24T11:52:34.000Z","size":49,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-13T16:57:08.552Z","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/lorefnon.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}},"created_at":"2022-12-10T18:25:01.000Z","updated_at":"2024-10-24T11:52:38.000Z","dependencies_parsed_at":"2025-04-10T23:42:50.125Z","dependency_job_id":"cf46ebef-d88b-4b2a-8cb4-2acf4549cdb1","html_url":"https://github.com/lorefnon/overridable-fn","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/lorefnon/overridable-fn","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Foverridable-fn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Foverridable-fn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Foverridable-fn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Foverridable-fn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lorefnon","download_url":"https://codeload.github.com/lorefnon/overridable-fn/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Foverridable-fn/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29062483,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-03T23:14:54.203Z","status":"ssl_error","status_checked_at":"2026-02-03T23:14:50.873Z","response_time":96,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":[],"created_at":"2024-11-07T00:09:14.076Z","updated_at":"2026-02-04T00:02:48.965Z","avatar_url":"https://github.com/lorefnon.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# overridable-fn\n\nCreate functions whose behavior can be swapped out dynamically.\n\nIntended to be a lightweight less-intrusive alternative to IoC containers.\n\nLet's say you have this function:\n\n```ts\nconst getUserById = async (id: string) =\u003e {\n    // ... fetch users from database\n}\n```\n\nNow if you just wrap this function with fn:\n\n```ts\nimport { fn } from \"overridable-fn\"\n\nconst getUserById = fn(async (id: string) =\u003e {\n    // ... fetch users from database\n})\n```\n\nThe behavior of `getUserById` remains unchanged\n\n```ts\ngetUserById(10) //-\u003e fetches users from database\n```\n\nHowever, you can now override the implementation (eg. in tests)\n\n```ts\ngetUserById.override((prevImpl) =\u003e async (id: string) =\u003e {\n    console.log('[WARN] Returning fake user')\n    return new User({ id })\n})\n```\n\nSo if you call `getUserById()` now, your overriden implementation be invoked instead.\n\n**Note:** fn returns a wrapped function - it does not modify the function passed to it. So after wrapping you must always invoke the wrapper returned by fn. \n\n```ts\nconst getUserByIdImpl = async (id: string) =\u003e { /* Get user from database */ }\nconst getUserById = fn(getUserByIdImpl) // getUserById is a wrapper which invokes getUserByIdImpl\ngetUserById.override(() =\u003e async (id: string) =\u003e { /* Return fake user */ })\ngetUserById() // Returns fake user\ngetUserByIdImpl() // Returns user from database, because wrapper is not used\n```\n\nYou can access the wrapped function by using `unwrap`. Calling `unwrap` does not change the wrapper anyhow.\n\n```ts\nconst getUserByIdImpl = async (id: string) =\u003e { /* Get user from database */ }\nconst getUserById = fn(getUserByIdImpl) // getUserById is a wrapper which invokes getUserByIdImpl\ngetUserById.unwrap() === getUserByIdImpl // true\ngetUserById.override(() =\u003e async (id: string) =\u003e { /* Return fake user */ })\ngetUserById.unwrap() === getUserByIdImpl // false\n```\n\n```ts\n$ getUserById(10)\n[WARN] Returning fake user\n```\n\nThe handle returned from override has a restore function that can be used to restore the previous implementation. \n\n```ts\ndescribe('Your feature', () =\u003e {\n    let restore;\n\n    before(() =\u003e {\n        ({ restore } = getUserById.override((prevImpl) =\u003e async () =\u003e {\n            // ... mock implementation\n        }))\n    })\n\n    after(() =\u003e {\n        restore?.()\n    })\n\n    test(\"use overriden impl\", async () =\u003e {\n        const fakeUser = await getUserById(10)\n    })\n})\n```\n\nPlease note that the handle returned from the override is able to restore only to the implementation before the override.\n\n```ts\nconst wrapper = fn(() =\u003e 1)\nwrapper.override(() =\u003e () =\u003e 2)\nconst { restore } = wrapper.override(() =\u003e () =\u003e 3)\nrestore()\nwrapper() // 2\nrestore() // Repeated calls to restore have no further effect\nwrapper() // 2\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florefnon%2Foverridable-fn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Florefnon%2Foverridable-fn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florefnon%2Foverridable-fn/lists"}