Skip to content

Commit

Permalink
🦺 Validate ResolvedIndex
Browse files Browse the repository at this point in the history
* Adds validation on the `newResolvedIndex` created by rollcall
  • Loading branch information
mistryrn committed Nov 29, 2022
1 parent e2342bc commit 2727df1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
13 changes: 9 additions & 4 deletions src/external/rollCall/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fetch from "node-fetch";
import {
IndexReleaseRequest,
CreateResolvableIndexRequest,
ResolvedIndexSchema,
ResolvedIndex,
RollCallClient,
} from "./types";
Expand Down Expand Up @@ -29,13 +30,17 @@ const createRollcallClient = (config: RollcallConfig): RollCallClient => {
};

try {
const newResolvedIndex = (await fetch(url, {
const newResolvedIndex = await fetch(url, {
method: "POST",
body: JSON.stringify(req),
headers: { "Content-Type": "application/json" },
}).then((res) => res.json())) as ResolvedIndex;

return newResolvedIndex;
}).then(async (res) => {
if (!res.ok) {
throw new Error(`Error while creating new index: ${res.statusText}`);
}
return await res.json();
});
return ResolvedIndexSchema.parse(newResolvedIndex);
} catch (err) {
logger.error("Failed to get new resolved index from rollcall: " + err);
throw err;
Expand Down
24 changes: 14 additions & 10 deletions src/external/rollCall/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { z } from "zod";

// Rollcall builds the index name as `entity_type_shardPrefix_shard_release_prefix_release`,
// release is not in the request because rollcall will calculate it
export type CreateResolvableIndexRequest = {
Expand All @@ -16,16 +18,18 @@ export type IndexReleaseRequest = {
shards: string[];
};

export type ResolvedIndex = {
indexName: string;
entity: string;
type: string;
shardPrefix: string;
shard: string;
releasePrefix: string;
release: string;
valid: boolean;
};
export const ResolvedIndexSchema = z.object({
indexName: z.string(),
entity: z.string(),
type: z.string(),
shardPrefix: z.string(),
shard: z.string(),
releasePrefix: z.string(),
release: z.string(),
valid: z.boolean(),
});

export type ResolvedIndex = z.infer<typeof ResolvedIndexSchema>;

export type RollCallClient = {
createNewResolvableIndex: (
Expand Down

0 comments on commit 2727df1

Please sign in to comment.