{"id":15024592,"url":"https://github.com/starhoshi/event-response","last_synced_at":"2026-01-05T03:44:33.171Z","repository":{"id":85967058,"uuid":"121094393","full_name":"starhoshi/event-response","owner":"starhoshi","description":"Handle the results of Cloud Functions like http status code and response.","archived":false,"fork":false,"pushed_at":"2018-05-15T12:50:13.000Z","size":937,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-27T20:49:21.383Z","etag":null,"topics":["cloud-functions","firebase","firestore","javascript","npm","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/starhoshi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2018-02-11T06:46:29.000Z","updated_at":"2019-09-29T04:11:20.000Z","dependencies_parsed_at":"2023-05-21T03:00:46.330Z","dependency_job_id":null,"html_url":"https://github.com/starhoshi/event-response","commit_stats":{"total_commits":63,"total_committers":2,"mean_commits":31.5,"dds":"0.11111111111111116","last_synced_commit":"a39b18a4238f736fd3f3b073f1390b0f23b31e9e"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhoshi%2Fevent-response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhoshi%2Fevent-response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhoshi%2Fevent-response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhoshi%2Fevent-response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/starhoshi","download_url":"https://codeload.github.com/starhoshi/event-response/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244937832,"owners_count":20535127,"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":["cloud-functions","firebase","firestore","javascript","npm","typescript"],"created_at":"2024-09-24T20:00:37.231Z","updated_at":"2026-01-05T03:44:33.110Z","avatar_url":"https://github.com/starhoshi.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/starhoshi/event-response/master/docs/logo.png\" /\u003e\n\u003c/p\u003e\n\n# event-response\n\n[![npm version](https://badge.fury.io/js/event-response.svg)](https://badge.fury.io/js/event-response)\n[![Build Status](https://travis-ci.org/starhoshi/event-response.svg?branch=master)](https://travis-ci.org/starhoshi/event-response)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/9ff0f9753f4c4217b20c5eec7b25f7af)](https://www.codacy.com/app/kensuke1751/event-response?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=starhoshi/event-response\u0026amp;utm_campaign=Badge_Grade)\n[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)\n\nWhen you use Cloud Functions's [Background Functions](https://cloud.google.com/functions/docs/writing/background), have you ever wanted to record success / failure respond to the client side that it succeeded?\n\nevent-response is easy to understand like success status of HTTP and can record success / failure. It is also possible to handle it on the client side using the recorded status.\n\n## Install\n\n```\nyarn install event-response\n```\n\n## Overview\n\n### OK\n\nWhen Cloud Functions completes successfully, call `setOK()`.\n\nIt is 200 in http.\n\n```ts\nnew EventResponse.Result(user, 'prefix').setOK()\n```\n\n\u003cimg src=\"https://raw.githubusercontent.com/starhoshi/event-response/master/docs/ok.png\" width='70%' /\u003e\n\n### Bad Request\n\nWhen Cloud Functions fails on client side problems such as invalid parameters, call `setBadRequest()`. And you can set `id`, `error`.\n\nIt is 400 in http.\n\n```ts\nnew EventResponse.Result(user, 'prefix').setBadRequest('error_id', 'error reason')\n```\n\n\u003cimg src=\"https://raw.githubusercontent.com/starhoshi/event-response/master/docs/badrequest.png\" width='70%' /\u003e\n\n### Inernal Error\n\nIf an error occurs on the server side, call `setInternalError`. And you can set `id`, `error`.\n\nIt is 500 in http.\n\n```ts\nnew EventResponse.Result(user, 'prefix').setInternalError('error_id', 'error reason')\n```\n\n\u003cimg src=\"https://raw.githubusercontent.com/starhoshi/event-response/master/docs/internal.png\" width='70%' /\u003e\n\n\n## Usage\n\nThis sample is written in TypeScript.\n\n### 1. Initialize\n\nInitialize event-response in your index.ts.\n\n```ts\nimport * as EventResponse from 'event-response'\nimport * as functions from 'firebase-functions'\n\nEventResponse.initialize(functions.config().firebase)\n```\n\n### 2. Call set method\n\nYou can set 3 pattens.\n\n* OK\n* BadRequest\n* InternalError\n\n```ts\nexports.updateUser = functions.firestore.document('users/{userId}')\n  .onCreate(async event =\u003e {\n    if (!event.data.data().name) {\n      return new EventResponse.Result(event.data.ref, 'updateUser').setBadRequest('NameNotFound', 'User.name not found')\n    }\n\n    try {\n      await event.data.ref.update({name: 'new name'})\n      await new EventResponse.Result(event.data.ref, 'updateUser').setOK()\n    } catch (error) {\n      await new EventResponse.Result(event.data.ref, 'updateUser').setInternalError('NameUpdateFailed', error.toString())\n      return Promise.reject(error)\n    }\n\n    return undefined\n})\n```\n\nThe result can be got as follows.\n\n```ts\nadmin.firestore().doc('user/1000').get().then(s =\u003e {\n  const user = s.data())\n  const status = user.updateUserResult.status\n  const id = user.updateUserResult.id\n  const error = user.updateUserResult.error\n}\n```\n\n## Advanced\n\n### Failure\n\nTODO\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarhoshi%2Fevent-response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstarhoshi%2Fevent-response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarhoshi%2Fevent-response/lists"}