Skip to content

Commit

Permalink
Merge pull request #423 from MuckRock/typescript
Browse files Browse the repository at this point in the history
Type improvements
  • Loading branch information
eyeseast authored Feb 5, 2024
2 parents a3a062e + 17cb9ab commit f07c675
Show file tree
Hide file tree
Showing 37 changed files with 251 additions and 165 deletions.
1 change: 1 addition & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ jobs:
NODE_ENV: production
- uses: ArtiomTr/jest-coverage-report-action@v2
with:
annotations: none
test-script: npm run test-coverage
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,17 @@ jobs:
env:
NODE_ENV: production
- run: npm test
check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: "18.x"
- run: npm ci
- run: npm run build
env:
NODE_ENV: production
- run: npm run check
49 changes: 31 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"storybook": "7.6.10",
"storybook-addon-cookie": "^3.2.0",
"storybook-mock-date-decorator": "^1.0.1",
"svelte-check": "^3.6.3",
"svelte-jester": "^3.0.0",
"tape": "^5.7.2",
"ts-jest": "^29.1.2"
Expand All @@ -92,6 +93,7 @@
"build-serve": "cross-env NODE_ENV=production webpack && serve public -l 80 --single",
"build-analyze": "cross-env NODE_ENV=production-analyze webpack",
"build-storybook": "storybook build",
"check": "svelte-check --threshold error",
"dev": "concurrently \"npm:dev-app\" \"npm:dev-embed\"",
"dev-app": "cross-env NODE_ENV=development SUPPRESS_WARNINGS=1 webpack serve --config webpack.app.config.js",
"dev-embed": "cross-env NODE_ENV=development webpack --config webpack.embed.config.js",
Expand Down
6 changes: 3 additions & 3 deletions src/addons/dispatch/Premium.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
} from "../../manager/orgsAndUsers.js";
import type { AddOnListItem } from "../types";
import type { User } from "../../pages/app/accounts/types";
import { type User, isOrg } from "../../api/types/orgAndUser";
export let addon: AddOnListItem;
Expand All @@ -23,11 +23,11 @@
let spendingLimitEnabled = false;
let spendingLimit = 0;
$: creditBalance = user?.organization
$: creditBalance = isOrg(user?.organization)
? getCreditBalance(user.organization)
: 0;
$: isIndividualOrg =
typeof user?.organization !== "string" && user?.organization?.individual;
isOrg(user?.organization) && user?.organization?.individual;
$: isPremium = addon?.parameters?.categories?.includes("premium") ?? false;
const { amount, unit, price } = addon?.parameters?.cost ?? {};
Expand Down
2 changes: 1 addition & 1 deletion src/addons/dispatch/fields/ArrayField.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
};
export let count: number = 1;
export let value: [any] = Array(count).fill(null);
export let value: any[] = Array(count).fill(null);
$: numItems = value?.length ?? 0;
Expand Down
49 changes: 34 additions & 15 deletions src/api/orgAndUser.js → src/api/orgAndUser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import session, { cookiesEnabled, getCsrfToken } from "./session.js";
import { USER_EXPAND, ORG_EXPAND, DEFAULT_EXPAND } from "./common.js";
import { queryBuilder } from "@/util/url.js";
import { grabAllPages } from "@/util/paginate.js";
import { queryBuilder } from "../util/url.js";
import { grabAllPages } from "../util/paginate.js";
import { apiUrl } from "./base.js";
import { Nullable, Org, User } from "./types";

export async function getMe(expand = DEFAULT_EXPAND) {
export async function getMe(expand = DEFAULT_EXPAND): Promise<Nullable<User>> {
// Check that the user is logged in via cookies
if (cookiesEnabled && !getCsrfToken()) return null;
// Check that the user is logged in via network request
Expand All @@ -15,49 +16,64 @@ export async function getMe(expand = DEFAULT_EXPAND) {
return data;
}

export async function getUser(id, expand = DEFAULT_EXPAND) {
export async function getUser(
id: number,
expand = DEFAULT_EXPAND,
): Promise<User> {
const { data } = await session.get(
queryBuilder(apiUrl(`users/${id}/`), { expand }),
);
return data;
}

export async function changeActiveOrg(orgId) {
export async function changeActiveOrg(orgId: number): Promise<void> {
await session.patch(apiUrl(`users/me/`), { organization: orgId });
}

export async function getOrganization(id, expand = ORG_EXPAND) {
export async function getOrganization(
id: number,
expand = ORG_EXPAND,
): Promise<Org> {
const { data } = await session.get(
queryBuilder(apiUrl(`organizations/${id}/`), { expand }),
);
return data;
}

export async function getOrganizations(individual = null, expand = ORG_EXPAND) {
export async function getOrganizations(
individual = null,
expand = ORG_EXPAND,
): Promise<Org[]> {
const orgs = await grabAllPages(
queryBuilder(apiUrl(`organizations/`), { individual, expand }),
);
return orgs;
}

export async function getOrganizationsByIds(ids, expand = ORG_EXPAND) {
export async function getOrganizationsByIds(
ids: number[],
expand = ORG_EXPAND,
): Promise<Org[]> {
const orgs = await grabAllPages(
queryBuilder(apiUrl(`organizations/`), { id__in: ids, expand }),
);
return orgs;
}

export async function getUsers({ projectIds, orgIds }, expand = USER_EXPAND) {
export async function getUsers(
{ projectIds, orgIds }: { projectIds: number[]; orgIds: number[] },
expand = USER_EXPAND,
): Promise<User[]> {
if (projectIds != null && orgIds != null) {
throw new Error("Only specify one of project or org ids");
}

const query = { expand };
if (projectIds != null) {
query["project"] = projectIds.map((id) => `${id}`).join(",");
query["project"] = projectIds.map(String).join(",");
}
if (orgIds != null) {
query["organization"] = orgIds.map((id) => `${id}`).join(",");
query["organization"] = orgIds.map(String).join(",");
}

const users = await grabAllPages(queryBuilder(apiUrl(`users/`), query));
Expand All @@ -67,7 +83,7 @@ export async function getUsers({ projectIds, orgIds }, expand = USER_EXPAND) {
export async function autocompleteOrganizations(
prefix = "",
individual = false,
) {
): Promise<Org[]> {
const { data } = await session.get(
queryBuilder(apiUrl("organizations/"), {
name__istartswith: prefix,
Expand All @@ -78,7 +94,10 @@ export async function autocompleteOrganizations(
return data.results;
}

export async function autocompleteUsers(prefix = "", orgIds = null) {
export async function autocompleteUsers(
prefix = "",
orgIds = null,
): Promise<User> {
const { data } = await session.get(
queryBuilder(apiUrl("users/"), {
name__istartswith: prefix,
Expand All @@ -89,11 +108,11 @@ export async function autocompleteUsers(prefix = "", orgIds = null) {
return data.results;
}

export async function createMailkey() {
export async function createMailkey(): Promise<string> {
const { data } = await session.post(apiUrl("users/mailkey/"));
return data.mailkey;
}

export async function destroyMailkey() {
export async function destroyMailkey(): Promise<void> {
await session.delete(apiUrl("users/mailkey/"));
}
Loading

0 comments on commit f07c675

Please sign in to comment.