@goldenhippo/gh-surveys

中文文档
0.0.46发布于 2024/02/06UNLICENSEDTypeScriptESM + CJS近 30 天下载 3815

Initial survey package for Golden Hippo

原始使用文档
文档目录5 节
  1. Table of Contents
  2. Installation
  3. Usage
  4. SurveyAPI
  5. SurveyResponse

Golden Hippo Surveys

Table of Contents

Installation

npm i @goldenhippo/gh-surveys

This package requires that you have an instance of the GH Survey API deployed with a publicly accessible URL.

Usage

SurveyAPI

Import the SurveyAPI class from the package and instantiate it with a connection string to a deployed API. The connection string should be in the format of https://surveyapi.herokuapp.com (or whatever the deployed API URL is).

Note: There should be no slash at the end of the URL.

import { SurveyAPI } from "@goldenhippo/gh-surveys";

const surveyAPI = new SurveyAPI(
  "https://surveyapi.herokuapp.com"
);

SurveyAPI Methods

The SurveyAPI class has the following methods:

  • retrieve(sessionId: string) - Retrieves all responses for a given session ID. Returns a Promise that resolves to an array of SurveyRetrievalResponse objects.
  • ensureTableExists(create: boolean = false) - Confirms that the survey_responses table exists in the database. If it does not exist, the create parameter can be passed to create the table. Returns a Promise that resolves to a boolean indicating whether the table exists. This process should only be run on application startup because it is an expensive operation.
  • submitToDB(surveyResponse: SurveyResponse) - Submits a SurveyResponse object to the database. Returns a Promise that resolves to a SurveySubmitResponse object.

SurveyAPI Usage

import { SurveyAPI } from "@goldenhippo/gh-surveys";

const surveyAPI = new SurveyConnection(
  "https://surveyapi.herokuapp.com"
);

const responses = surveyAPI.retrieve("1234")
  .then((sessionResponses) => {
      console.log(sessionResponses);
      //Find the age response
      const ageResponse = sessionResponses.find(response => response instanceof SurveyResponse.Age)
      if(ageResponse) {
      const ageSelection = ageResponse.answer
      //Do something based on the answer which will be one of the enum options
    }
  })

const tableExists = await surveyAPI.ensureTableExists(); // Validate that table exists, but do not create it if it does not.
console.log(tableExists);

const tableExistsWithCreate = await surveyAPI.ensureTableExists(true); // Validate that table exists, and create it if it does not.
console.log(tableExistsWithCreate);

SurveyResponse

The SurveyResponse class is an abstract class that should be extended to create a response for a specific survey question type. The created object can then be submitted to the database using the SurveyAPI.submitToDB method and returns a promise that resolves to a SurveySubmitResponse object.

The response object has the following structure:

{
  success: boolean,
  response: {
    answer: string,
    sessionId: string,
    questionType: string
  } | null
}
import { SurveyAPI, SurveyResponse } from "@goldenhippo/gh-surveys";

const surveyAPI = new SurveyAPI(
  "https://surveyapi.herokuapp.com"
);

const ageResponse = new SurveyResponse.Age({
  question: "How old are you?",
  rawAnswer: "21",
  answer: "AGE_18_24",
  sessionId: "1234",
});
const ageSubmission = apiConnection.submitToDB(ageResponse)
.then((response) => {
  console.log(response);
  /*
   {
      success: true,
      response: {
        answer: "AGE_18_24",
        sessionId: "1234",
        questionType: "AGE"
      }
   }
   */
}

Question Types

The SurveyResponse class allows you to create responses for the following question types:

  • Age
  • Gender
  • DigestiveIssue
  • IssueAge
  • BowelMovementWeekly
  • SolutionDifficulty

Note: To view the available questions and answers, you can access the configuration file from your API URL at /config. For example, https://yourapi.herokuapp.com/config.

Answers

Each of the question types has a set of possible answers. The answers are defined as static properties on the class. For example, the SurveyResponse.Gender class has the following possible answers:

  • MALE
  • FEMALE
  • NON_BINARY
  • DECLINE_TO_ANSWER

Note: To view the available questions and answers, you can access the configuration file from your API URL at /config. For example, https://yourapi.herokuapp.com/config.

版本与发布记录

@goldenhippo/gh-surveys 共发布 48 个版本,首次发布于 2023/04/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.46latest2024/02/062 年前
0.0.452024/02/012 年前
0.0.442023/12/212 年前
0.0.432023/07/043 年前
0.0.422023/06/273 年前
0.0.412023/06/273 年前

其他发布通道:stable → 0.0.22,beta → 0.0.38-beta.0。

关于 @goldenhippo/gh-surveys 的常见问题

@goldenhippo/gh-surveys 是什么?

Initial survey package for Golden Hippo

如何安装 @goldenhippo/gh-surveys?

在项目目录运行 npm install @goldenhippo/gh-surveys,或使用 pnpm add / yarn add / bun add。

@goldenhippo/gh-surveys 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@goldenhippo/gh-surveys 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@goldenhippo/gh-surveys 可以商用吗?

@goldenhippo/gh-surveys 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@goldenhippo/gh-surveys 的下载量有多少?

根据 npm 官方下载统计,@goldenhippo/gh-surveys 在 2026/10/17 – 2026/11/15 这 30 天内被下载 3,815 次,日均约 127 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@goldenhippo/gh-surveys 还在维护吗?

最新版本 0.0.46 发布于 2024/02/06(2 年前);至今共发布 48 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/04/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。