金色希波调查
目录
安装
npm i @goldenhippo/gh-surveys
此软件包需要您使用一个可公开访问的 URL 配置 GH 调查 API 实例 .
使用量
调查API
从软件包中导入 SurveyAPI 类,并用连接字符串将其即时化为已部署的API. 连接字符串的格式应该是: https://surveyapi.herokuapp.com (或无论部署的API URL是什么).
注:URL末尾不应有斜线.
import { SurveyAPI } from "@goldenhippo/gh-surveys";
const surveyAPI = new SurveyAPI(
"https://surveyapi.herokuapp.com"
);
A. 调查方法
调查API类别有以下方法:
retrieve(sessionId: string)- 获取特定会话ID的所有回复。 返回解析到一系列测量检索回复对象的保证 .ensureTableExists(create: boolean = false)- 确认数据库中存在调查-答复表。 如果它不存在,则create参数可以被传递来创建表格。 返回一个解析为布尔表示表格是否存在的保证。 这个过程只应在应用程序启动时运行,因为它是一个昂贵的操作.submitToDB(surveyResponse: SurveyResponse)- 向数据库提交调查答复对象。 返回解决 SurveySubmitResponse 对象的保证.
调查API 使用情况
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);
调查答复
调查响应类是一个抽象的类,应当被扩展,为特定的调查问题类型创建一个响应. 创建对象然后可以使用 SurveyAPI.submitToDB 方法并返回解析到 SurveySubmitResponse 对象.
反应对象的结构如下:
{
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"
}
}
*/
}
问题类型
调查响应类允许您为下列问题类型创建响应:
- 年龄
- 性别
- 消化问题
- 问题时代
- 周而复始
- 解决方案
注意: 要查看可用的问答, 您可以从您的 API URL 访问配置文件 /config。例如, https://yourapi.herokuapp.com/config。 。 。 .
答复
每个问题类型都有一套可能的答案. 答案被定义为类上的静态属性. 例如 SurveyResponse.Gender 类有以下可能的答案:
- 马里
- 妇女
- 非两年期
- 解禁线 TO ANSWER
注意: 要查看可用的问答, 您可以从您的 API URL 访问配置文件 /config。例如, https://yourapi.herokuapp.com/config。 。 。 .