Skip to content

Commit

Permalink
Child and past sessions data (#18)
Browse files Browse the repository at this point in the history
* Remove unneeded package

* Add lookit-api package

* Add functions to get child and past sessions data

* Add lookit helpers package

* Expose child and past sessions data

* Updated helper to be called by researchers

* Add tests for coverage

* Add text to tests

* Update readme to address inter-package linking

* Update to helper package config

* changeset
  • Loading branch information
okaycj authored Feb 21, 2024
1 parent 8d96918 commit f528fb3
Show file tree
Hide file tree
Showing 22 changed files with 653 additions and 351 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-beans-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lookit/lookit-initjspsych": patch
---

Update to package config
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
"ignore": ["@lookit/lookit-api"]
}
5 changes: 5 additions & 0 deletions .changeset/sharp-fishes-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lookit/lookit-helpers": patch
---

Initial deploy
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ And add the following to `package.json`:
],
```

Update the following in `package.json`:

```json
"main": "dist/index.js",
```

At the root of the new package run the following commands:

```sh
Expand Down
710 changes: 363 additions & 347 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
"homepage": "https://github.com/lookit/lookit-jspsych#readme",
"dependencies": {
"@changesets/cli": "^2.27.1",
"@lookit/lookit-initjspsych": "^1.0.0",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"prettier": "^3.1.1"
"prettier": "^3.1.1",
"jest-fetch-mock": "^3.0.3"
},
"workspaces": [
"packages/*"
Expand Down
3 changes: 3 additions & 0 deletions packages/lookit-api/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const config = require("@jspsych/config/jest").makePackageConfig(__dirname);
config.moduleNameMapper = {};
module.exports = config;
15 changes: 15 additions & 0 deletions packages/lookit-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@lookit/lookit-api",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"test": "jest --coverage",
"build": "rollup --config"
},
"author": "",
"license": "ISC",
"dependencies": {
"@jspsych/config": "^2.0.0"
}
}
3 changes: 3 additions & 0 deletions packages/lookit-api/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { makeRollupConfig } from "@jspsych/config/rollup";

export default makeRollupConfig("lookitAPI");
30 changes: 30 additions & 0 deletions packages/lookit-api/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { enableFetchMocks } from "jest-fetch-mock";

import api from "./index";
import { ApiResponse, Child, PastSession } from "./types";

enableFetchMocks();

test("Show that retrieveChild is responding with expected data", async () => {
// Use date as id to show that the data isn't manufactured.
const child = { id: new Date().toString() } as Child;
const data: ApiResponse<Child> = { data: child };

fetchMock.mockOnce(JSON.stringify(data));

const retrieveChild = await api.retrieveChild("some uuid");
expect(child).toStrictEqual(retrieveChild);
});

test("Show that retrievePastSessions is responding with expected data", async () => {
// Use date as id to show that the data isn't manufactured.
const pastSessions: PastSession[] = [
{ id: new Date().toString() } as PastSession,
];
const data: ApiResponse<PastSession[]> = { data: pastSessions };

fetchMock.mockOnce(JSON.stringify(data));

const retrievePastSessions = await api.retrievePastSessions("some uuid");
expect(pastSessions).toStrictEqual(retrievePastSessions);
});
12 changes: 12 additions & 0 deletions packages/lookit-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiResponse, Child, PastSession } from "./types";
import { get } from "./utils";

async function retrieveChild(uuid: string) {
return (await get<ApiResponse<Child>>(`children/${uuid}/`)).data;
}

async function retrievePastSessions(uuid: string) {
return (await get<ApiResponse<PastSession[]>>(`past-sessions/${uuid}/`)).data;
}

export default { retrieveChild, retrievePastSessions };
76 changes: 76 additions & 0 deletions packages/lookit-api/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
export type Relationship = {
links: {
related: string;
};
};

export type Attributes = {
readonly pk?: number;
readonly url?: string;
};

export interface Relationships {}

export interface ChildAttrs extends Attributes {
given_name: string;
birthday: string;
gender: "m" | "f" | "o" | "na";
readonly age_at_birth?: string;
additional_information?: string;
readonly language_list?: string;
readonly condition_list?: string;
deleted?: boolean;
former_lookit_profile_id?: string;
readonly pk?: number;
}

export interface PastSessionAttrs extends Attributes {
conditions?: Record<string, never>;
global_event_timings?: Record<string, never>;
exp_data?: Record<string, never>;
sequence?: string[];
completed?: boolean;
completed_consent_frame?: boolean;
survey_consent?: boolean;
readonly created_on?: string;
is_preview?: boolean;
readonly hash_child_id?: string;
recording_method?: string;
eligibility?: (
| "Eligible"
| "Ineligible_TooYoung"
| "Ineligible_TooOld"
| "Ineligible_CriteriaExpression"
| "Ineligible_Participation"
)[];
}

export interface Data<Attributes> {
type: string;
id: string;
attributes: Attributes;
relationships: Relationships;
links: {
self: string;
};
}

export interface ApiResponse<Data> {
data: Data;
}

export interface Child extends Data<ChildAttrs> {
type: "children";
relationships: {
user: Relationship;
};
}
export interface PastSession extends Data<PastSessionAttrs> {
type: "past_sessions";
relationships: {
child: Relationship;
user: Relationship;
study: Relationship;
demographic_snapshot: Relationship;
};
}
15 changes: 15 additions & 0 deletions packages/lookit-api/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const url_base = "/api/v2/";

export async function get<T>(url: string) {
/**
* Function for REST get.
*/

const request = new Request(url_base + url, {
method: "GET",
mode: "same-origin",
});

const response = await fetch(request);
return response.json() as Promise<T>;
}
8 changes: 8 additions & 0 deletions packages/lookit-api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@jspsych/config/tsconfig.core.json",
"compilerOptions": {
"strict": true,
"baseUrl": "."
},
"include": ["src"]
}
3 changes: 3 additions & 0 deletions packages/lookit-helpers/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const config = require("@jspsych/config/jest").makePackageConfig(__dirname);
config.moduleNameMapper = {};
module.exports = config;
20 changes: 20 additions & 0 deletions packages/lookit-helpers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@lookit/lookit-helpers",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"test": "jest --coverage",
"build": "rollup --config"
},
"unpkg": "dist/index.browser.min.js",
"files": [
"src",
"dist"
],
"author": "",
"license": "ISC",
"dependencies": {
"@jspsych/config": "^2.0.0"
}
}
3 changes: 3 additions & 0 deletions packages/lookit-helpers/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { makeRollupConfig } from "@jspsych/config/rollup";

export default makeRollupConfig("lookitHelpers");
40 changes: 40 additions & 0 deletions packages/lookit-helpers/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ApiResponse, Child, PastSession } from "@lookit/lookit-api/dist/types";
import { enableFetchMocks } from "jest-fetch-mock";

import Helpers from "./index";

enableFetchMocks();

test("Show that helpers child method returns expected data", async () => {
// Use date as given name to show that the data isn't manufactured.
const child = { attributes: { given_name: new Date().toString() } } as Child;
const data: ApiResponse<Child> = { data: child };

fetchMock.mockOnce(JSON.stringify(data));

const child_uuid = "child uuid";
const response_uuid = "response uuid";
const helpersChild = await new Helpers(child_uuid, response_uuid).child();
expect({
given_name: child.attributes.given_name,
additional_information: child.attributes.additional_information,
age_at_birth: child.attributes.age_at_birth,
birthday: child.attributes.birthday,
}).toStrictEqual(helpersChild);
});

test("Show that helpers past sessions returns expected data", async () => {
// Use date as id to show that the data isn't manufactured.
const pastSessions = [{ id: new Date().toString() }] as PastSession[];
const data: ApiResponse<PastSession[]> = { data: pastSessions };

fetchMock.mockOnce(JSON.stringify(data));

const child_uuid = "child uuid";
const response_uuid = "response uuid";
const helpersPastSessions = await new Helpers(
child_uuid,
response_uuid,
).pastSessions();
expect(pastSessions).toStrictEqual(helpersPastSessions);
});
26 changes: 26 additions & 0 deletions packages/lookit-helpers/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import api from "@lookit/lookit-api";

import { ChildSubSet } from "./types";

class Helpers {
child_uuid: string;
response_uuid: string;

constructor(child_uuid: string, response_uuid: string) {
this.child_uuid = child_uuid;
this.response_uuid = response_uuid;
}

async child(): Promise<ChildSubSet> {
const child = await api.retrieveChild(this.child_uuid);
const { given_name, birthday, age_at_birth, additional_information } =
child.attributes;
return { given_name, birthday, age_at_birth, additional_information };
}

async pastSessions() {
return await api.retrievePastSessions(this.response_uuid);
}
}

export default Helpers;
8 changes: 8 additions & 0 deletions packages/lookit-helpers/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Child } from "@lookit/lookit-api/dist/types";

export type ChildSubSet = {
given_name: Child["attributes"]["given_name"];
birthday: Child["attributes"]["birthday"];
age_at_birth?: Child["attributes"]["age_at_birth"];
additional_information?: Child["attributes"]["additional_information"];
};
8 changes: 8 additions & 0 deletions packages/lookit-helpers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@jspsych/config/tsconfig.core.json",
"compilerOptions": {
"strict": true,
"baseUrl": "."
},
"include": ["src"]
}
2 changes: 1 addition & 1 deletion packages/lookit-initjspsych/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@lookit/lookit-initjspsych",
"version": "1.0.4",
"description": "",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"test": "jest --coverage",
"build": "rollup --config"
Expand Down

0 comments on commit f528fb3

Please sign in to comment.