{"id":22740250,"url":"https://github.com/mtzaquia/bifrost","last_synced_at":"2026-04-09T10:19:44.922Z","repository":{"id":85860101,"uuid":"381995011","full_name":"mtzaquia/bifrost","owner":"mtzaquia","description":"Bifrost is a lightweight, scalable framework for interacting with JSON, REST APIs.","archived":false,"fork":false,"pushed_at":"2024-01-06T14:11:31.000Z","size":56,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-09T06:07:28.916Z","etag":null,"topics":["api","json","lightweight","networking","request","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","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/mtzaquia.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":"2021-07-01T10:26:36.000Z","updated_at":"2024-01-06T14:11:19.000Z","dependencies_parsed_at":"2024-01-06T15:28:00.111Z","dependency_job_id":"cd6f4f88-eae7-4ae2-a532-18e04c20e8bf","html_url":"https://github.com/mtzaquia/bifrost","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/mtzaquia/bifrost","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtzaquia%2Fbifrost","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtzaquia%2Fbifrost/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtzaquia%2Fbifrost/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtzaquia%2Fbifrost/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mtzaquia","download_url":"https://codeload.github.com/mtzaquia/bifrost/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtzaquia%2Fbifrost/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264403799,"owners_count":23602621,"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":["api","json","lightweight","networking","request","swift"],"created_at":"2024-12-10T23:08:01.890Z","updated_at":"2026-04-09T10:19:44.913Z","avatar_url":"https://github.com/mtzaquia.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Bifrost\n\nBifrost is a lightweight, scalable framework for interacting with JSON, REST APIs.\n\n## Instalation\n\nBifrost is available via Swift Package Manager.\n\n```swift\ndependencies: [\n  .package(url: \"https://github.com/mtzaquia/bifrost.git\", from: \"3.0.0\"),\n],\n```\n\n## Usage\n\n### API\n\nSimply declare an entity conforming to `API` to start, then fulfill the required protocol conformances:\n\n```swift\nstruct MyAPI: API {\n  let baseURL: URL = URL(string: \"https://api.myapi.com/v2/\")!\n  // ...\n}\n``` \n\nYou can define default parameters and headers that will apply to all requests. You can also configure the decoder for your specific use-case.\n\n```swift\nstruct MyAPI: API {\n  // ...\n  func queryParameters() -\u003e [URLQueryItem]\n    [\n      URLQueryItem(name: \"api-key\", value: \"\u003cmy secret key\u003e\")\n    ]\n  }\n\n  var jsonDecoder: JSONDecoder = {\n    let jd = JSONDecoder()\n    jd.dateDecodingStrategy = .iso8601\n    return jd\n  }()\n}\n```\n\n### Requests\n\nFor each request, create a type with its supported parameters. Make sure this type conforms to `Requestable`. \nYou can also provide default header fields for a specific request if needed, and you can choose the HTTP method for that request. \n\n```swift\nstruct MyRequest {\n  private(set) var name: String\n  private(set) var anotherParam: String?\n}\n\nextension MyRequest: Requestable {\n  var path: String { \"api/my-request\" }\n  \n  struct Response: Decodable {\n    let results: [MyResultObject]\n  }\n}\n``` \n\n\u003e [!NOTE]\n\u003e If you expect an empty response, the built-in `EmptyResponse` type is avaiable for convenience.\n\n### Making the call\n\nFinally, you are ready to submit a request! Concurrency allows you to inline your call easily: \n\n```swift\n// ...\nlet response = try await MyAPI().response(for: MyRequest(name: \"My fancy name\"))\nprint(response.results) // Our response is already a Swift type! More specifically, an instance of `MyRequest.Response`.\n```\n\n### Intercepting requests and responses\n\nYou can define request and response interceptors on your API for request mutation, mocking, and response post-processing.\n\n- `requestInterceptors` run before Bifrost builds the final `URLRequest`\n- `responseInterceptors` run after Bifrost has either decoded a network response or received a mocked response from a request interceptor\n- both phases use `InterceptionResult\u003cT\u003e` with `.continue` and `.return(...)`\n- mocked and real responses share the same `InterceptedResponse\u003cResponse\u003e` wrapper, which exposes `body`, `httpResponse`, `statusCode`, and normalized `headerFields`\n- response interceptors receive a `retry()` closure that reruns the original request pipeline\n- unsuccessful HTTP statuses are surfaced after the response phase, so response interceptors can recover from responses like `401`\n\n```swift\nstruct AddAuthorization: RequestInterceptor {\n  let token: String\n\n  func intercept\u003cRequest\u003e(\n    _ request: inout Request\n  ) async throws -\u003e InterceptionResult\u003cInterceptedResponse\u003cRequest.Response\u003e\u003e where Request: Requestable {\n    if var authenticated = request as? MyRequest {\n      authenticated.token = token\n      request = authenticated as! Request\n    }\n\n    return .continue\n  }\n}\n\nstruct RewriteResponse: ResponseInterceptor {\n  func intercept\u003cResponse\u003e(\n    _ response: inout InterceptedResponse\u003cResponse\u003e,\n    retry: () async throws -\u003e InterceptedResponse\u003cResponse\u003e\n  ) async throws -\u003e InterceptionResult\u003cInterceptedResponse\u003cResponse\u003e\u003e {\n    return .continue\n  }\n}\n\nstruct MyAPI: API {\n  let baseURL = URL(string: \"https://api.myapi.com/v2/\")!\n\n  var requestInterceptors: [any RequestInterceptor] {\n    [AddAuthorization(token: \"\u003ctoken\u003e\")]\n  }\n\n  var responseInterceptors: [any ResponseInterceptor] {\n    [RewriteResponse()]\n  }\n}\n```\n\nRequest interceptors mutate the request model itself, not `URLRequest`. That means any change they make still flows through the normal Bifrost request-building logic for path, query, headers, and body.\n\n```swift\nstruct MockUser: RequestInterceptor {\n  func intercept\u003cRequest\u003e(\n    _ request: inout Request\n  ) async throws -\u003e InterceptionResult\u003cInterceptedResponse\u003cRequest.Response\u003e\u003e where Request: Requestable {\n    guard request is GetUserRequest else {\n      return .continue\n    }\n\n    let httpResponse = HTTPURLResponse(\n      url: URL(string: \"https://api.myapi.com/v2/user\")!,\n      statusCode: 200,\n      httpVersion: nil,\n      headerFields: [\"X-Mocked\": \"true\"]\n    )!\n\n    return .return(\n      InterceptedResponse(\n        body: GetUserRequest.Response(name: \"Mocked User\") as! Request.Response,\n        httpResponse: httpResponse\n      )\n    )\n  }\n}\n```\n\nResponse interceptors always receive the full intercepted response, including metadata, so they can make decisions based on the HTTP code or headers as well as the decoded body. They can also call `retry()` to rerun request interception, request building, and transport after doing recovery work like refreshing a token.\n\n```swift\nstruct NormalizeUser: ResponseInterceptor {\n  func intercept\u003cResponse\u003e(\n    _ response: inout InterceptedResponse\u003cResponse\u003e,\n    retry: () async throws -\u003e InterceptedResponse\u003cResponse\u003e\n  ) async throws -\u003e InterceptionResult\u003cInterceptedResponse\u003cResponse\u003e\u003e where Response: Decodable {\n    if response.statusCode == 202 {\n      response.httpResponse = HTTPURLResponse(\n        url: response.httpResponse.url!,\n        statusCode: 200,\n        httpVersion: nil,\n        headerFields: response.headerFields\n      )!\n    }\n\n    return .continue\n  }\n}\n```\n\n## License\n\nCopyright (c) 2025 @mtzaquia\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtzaquia%2Fbifrost","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmtzaquia%2Fbifrost","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtzaquia%2Fbifrost/lists"}