Skip to content

Commit

Permalink
Merge branch 'master' into malik.8811.cleanup-ChainInfo-type
Browse files Browse the repository at this point in the history
  • Loading branch information
mzparacha committed Sep 10, 2024
2 parents e1f25e7 + f630bff commit ad2819c
Show file tree
Hide file tree
Showing 156 changed files with 2,288 additions and 1,374 deletions.
56 changes: 28 additions & 28 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -259,34 +259,34 @@ jobs:
run: pnpm lint-branch


# detect-broken-ts-expect-error:
# name: Detect Broken TS expect errors
# runs-on: ubuntu-latest
# timeout-minutes: 10
# strategy:
# matrix:
# node: [ 20 ]
#
# steps:
# - uses: actions/checkout@v4
# with:
# fetch-depth: 0
# - uses: ./.github/actions/setup
# with:
# node-version: ${{ matrix.node }}
#
# - name: Assert pnpm-lock.yaml is correct
# run: |
# if ! git diff --quiet; then
# echo 'You need to update the pnpm-lock.yaml file (potentially after installing node-gyp)'
# exit 1
# fi
#
# - name: build
# run: pnpm -r build
#
# - name: run detect-broken-ts-expect-error.sh
# run: cd packages/commonwealth && ./scripts/detect-broken-ts-expect-error.sh
detect-broken-ts-expect-error:
name: Detect Broken TS expect errors
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
matrix:
node: [ 20 ]

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: ./.github/actions/setup
with:
node-version: ${{ matrix.node }}

- name: Assert pnpm-lock.yaml is correct
run: |
if ! git diff --quiet; then
echo 'You need to update the pnpm-lock.yaml file (potentially after installing node-gyp)'
exit 1
fi
- name: build
run: pnpm -r build

- name: run detect-broken-ts-expect-error.sh
run: cd packages/commonwealth && ./scripts/detect-broken-ts-expect-error.sh

# These tests run quickly, so run them in a separate job
commonwealth-unit-integration:
Expand Down
6 changes: 3 additions & 3 deletions libs/adapters/src/express/command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandMetadata, User } from '@hicommonwealth/core';
import type { Metadata, User } from '@hicommonwealth/core';
import * as core from '@hicommonwealth/core';
import type { NextFunction, Request, RequestHandler, Response } from 'express';
import { ZodSchema } from 'zod';
Expand All @@ -12,8 +12,8 @@ import { ZodSchema } from 'zod';
* @returns express command handler
*/
export const command =
<Input extends ZodSchema, Output extends ZodSchema>(
md: CommandMetadata<Input, Output>,
<Input extends ZodSchema, Output extends ZodSchema, AuthContext>(
md: Metadata<Input, Output, AuthContext>,
): RequestHandler =>
async (req: Request, res: Response, next: NextFunction) => {
try {
Expand Down
6 changes: 3 additions & 3 deletions libs/adapters/src/express/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { QueryMetadata, User } from '@hicommonwealth/core';
import type { Metadata, User } from '@hicommonwealth/core';
import * as core from '@hicommonwealth/core';
import type { NextFunction, Request, RequestHandler, Response } from 'express';
import { ZodSchema, z } from 'zod';
Expand All @@ -9,8 +9,8 @@ import { ZodSchema, z } from 'zod';
* @returns express query handler
*/
export const query =
<Input extends ZodSchema, Output extends ZodSchema>(
md: QueryMetadata<Input, Output>,
<Input extends ZodSchema, Output extends ZodSchema, AuthContext>(
md: Metadata<Input, Output, AuthContext>,
): RequestHandler =>
async (
req: Request,
Expand Down
21 changes: 15 additions & 6 deletions libs/adapters/src/knock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import {
NotificationsProviderSchedulesReturn,
NotificationsProviderTriggerOptions,
} from '@hicommonwealth/core';
import { MAX_RECIPIENTS_PER_WORKFLOW_TRIGGER } from '@hicommonwealth/shared';
import { Knock, Schedule } from '@knocklabs/node';
import { ScheduleRepeatProperties } from '@knocklabs/node/dist/src/resources/workflows/interfaces';
import _ from 'lodash';
import { config } from '../config';

const MAX_RECIPIENTS_PER_WORKFLOW_TRIGGER = 1_000;

const log = logger(import.meta);

function formatScheduleResponse(
Expand Down Expand Up @@ -68,17 +67,18 @@ export function KnockProvider(): NotificationsProvider {
return config.PUSH_NOTIFICATIONS.KNOCK_APNS_CHANNEL_ID;
}
}

return {
name: 'KnockProvider',
dispose: () => Promise.resolve(),
async triggerWorkflow(
options: NotificationsProviderTriggerOptions,
): Promise<boolean> {
): Promise<PromiseSettledResult<{ workflow_run_id: string }>[]> {
// disable webhook workflow in all environments except production
// this is to prevent sending webhooks to real endpoints in all other env
if (options.key === 'webhooks' && !config.NOTIFICATIONS.WEBHOOKS.SEND) {
log.warn('Webhooks disabled');
return true;
return [];
}

const recipientChunks = _.chunk(
Expand All @@ -94,8 +94,17 @@ export function KnockProvider(): NotificationsProvider {
});
});

await Promise.all(triggerPromises);
return true;
const res = await Promise.allSettled(triggerPromises);
res.forEach((r) => {
if (r.status === 'rejected') {
log.error('KNOCK PROVIDER: Failed to trigger workflow', undefined, {
workflow_key: options.key,
data: options.data,
reason: JSON.stringify(r.reason),
});
}
});
return res;
},
async getMessages(
options: NotificationsProviderGetMessagesOptions,
Expand Down
19 changes: 13 additions & 6 deletions libs/adapters/src/trpc/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import {
command as coreCommand,
query as coreQuery,
handleEvent,
type CommandMetadata,
type EventSchemas,
type EventsHandlerMetadata,
type QueryMetadata,
type Metadata,
} from '@hicommonwealth/core';
import { TRPCError } from '@trpc/server';
import { ZodSchema, ZodUndefined, z } from 'zod';
Expand Down Expand Up @@ -45,8 +44,12 @@ const trpcerror = (error: unknown): TRPCError => {
* @param track analytics tracking metadata as tuple of [event, output mapper]
* @returns tRPC mutation procedure
*/
export const command = <Input extends ZodSchema, Output extends ZodSchema>(
factory: () => CommandMetadata<Input, Output>,
export const command = <
Input extends ZodSchema,
Output extends ZodSchema,
AuthContext,
>(
factory: () => Metadata<Input, Output, AuthContext>,
tag: Tag,
track?: Track<Output>,
) => {
Expand Down Expand Up @@ -75,8 +78,12 @@ export const command = <Input extends ZodSchema, Output extends ZodSchema>(
* @param tag query tag used for OpenAPI spec grouping
* @returns tRPC query procedure
*/
export const query = <Input extends ZodSchema, Output extends ZodSchema>(
factory: () => QueryMetadata<Input, Output>,
export const query = <
Input extends ZodSchema,
Output extends ZodSchema,
AuthContext,
>(
factory: () => Metadata<Input, Output, AuthContext>,
tag: Tag,
) => {
const md = factory();
Expand Down
1 change: 1 addition & 0 deletions libs/adapters/src/trpc/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export enum Tag {
LoadTest = 'LoadTest',
Wallet = 'Wallet',
Webhook = 'Webhook',
SuperAdmin = 'SuperAdmin',
}

export type Track<Output extends ZodSchema> = [
Expand Down
21 changes: 8 additions & 13 deletions libs/core/src/framework/command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { z, ZodError, ZodSchema } from 'zod';
import {
InvalidInput,
type CommandContext,
type CommandMetadata,
} from './types';
import { InvalidInput, type Context, type Metadata } from './types';

/**
* Generic command handler that adapts external protocols to conventional command context flows
Expand All @@ -19,22 +15,21 @@ import {
export const command = async <
Input extends ZodSchema,
Output extends ZodSchema,
AuthContext,
>(
{ input, auth, body }: CommandMetadata<Input, Output>,
{ actor, payload }: CommandContext<Input>,
{ input, auth, body }: Metadata<Input, Output, AuthContext>,
{ actor, payload }: Context<Input, AuthContext>,
validate = true,
): Promise<Partial<z.infer<Output>> | undefined> => {
): Promise<z.infer<Output> | undefined> => {
try {
const context: CommandContext<Input> = {
const context: Context<Input, AuthContext> = {
actor,
payload: validate ? input.parse(payload) : payload,
};
let state: Partial<z.infer<Output>> | undefined = undefined;
for (const fn of auth) {
// can use deep clone to make it pure
state = (await fn(context, state)) ?? state;
await fn(context);
}
return (await body(context, state)) ?? undefined;
return (await body(context)) ?? undefined;
} catch (error) {
if (error instanceof Error) {
if (error.name === 'ZodError') {
Expand Down
14 changes: 9 additions & 5 deletions libs/core/src/framework/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z, ZodError, ZodSchema } from 'zod';
import { InvalidInput, type QueryContext, type QueryMetadata } from './types';
import { InvalidInput, type Context, type Metadata } from './types';

/**
* Generic query handler that adapts external protocols to conventional query flow
Expand All @@ -11,13 +11,17 @@ import { InvalidInput, type QueryContext, type QueryMetadata } from './types';
* @returns query results
* @throws {@link InvalidInput} when user invokes query with invalid payload or attributes, or rethrows internal errors
*/
export const query = async <Input extends ZodSchema, Output extends ZodSchema>(
{ input, auth, body }: QueryMetadata<Input, Output>,
{ actor, payload }: QueryContext<Input>,
export const query = async <
Input extends ZodSchema,
Output extends ZodSchema,
AuthContext,
>(
{ input, auth, body }: Metadata<Input, Output, AuthContext>,
{ actor, payload }: Context<Input, AuthContext>,
validate = true,
): Promise<z.infer<Output> | undefined> => {
try {
const context: QueryContext<Input> = {
const context: Context<Input, AuthContext> = {
actor,
payload: validate
? Object.fromEntries(
Expand Down
Loading

0 comments on commit ad2819c

Please sign in to comment.