Skip to content

Commit

Permalink
Improve types (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rusty-Beard authored May 13, 2023
1 parent d65eb09 commit 98f2459
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 72 deletions.
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@8base/functions-types",
"version": "1.0.0",
"version": "1.0.1",
"description": "Types for 8base Custom Functions",
"author": "8base",
"license": "MIT",
Expand All @@ -10,11 +10,8 @@
"typecheck": "tsc --noEmit",
"postversion": "git push --follow-tags"
},
"devDependencies": {
"typescript": "^4.0.3"
},
"dependencies": {
"@graphql-typed-document-node/core": "^3.1.0",
"graphql": "^15.3.0"
"@graphql-typed-document-node/core": "^3.2.0",
"graphql": "^15.8.0"
}
}
123 changes: 71 additions & 52 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import { DocumentNode } from "graphql";
import { TypedDocumentNode } from "@graphql-typed-document-node/core";

export type AnyObject = Record<string, any>;
export type HttpHeaders = Record<string, string | undefined>;

export interface IDBObject {
id: string;
createdAt: Date;
updatedAt: Date;
deletedAt: number;
createdById: string | null;
}

export type GqlRequest = <
ResultT = Record<string, any>,
VariablesT = Record<string, any>
ResultT = AnyObject,
VariablesT = AnyObject
>(
query: DocumentNode | TypedDocumentNode<ResultT, VariablesT>,
query: string | DocumentNode | TypedDocumentNode<ResultT, VariablesT>,
variables?: VariablesT,
options?: {
checkPermissions?: boolean;
headers?: Record<string, any>;
headers?: HttpHeaders
}
) => Promise<ResultT>;

export type FunctionContext = {
api: {
gqlRequest: GqlRequest;
url: string;
};
invokeFunction: <
ResponseT extends InvokeFunctionResponse = InvokeFunctionResponse,
ArgsT = Record<string, any>
ArgsT = AnyObject
>(
name: string,
args?: ArgsT,
Expand All @@ -28,6 +40,7 @@ export type FunctionContext = {
workspaceId: string;
environmentId: string;
environmentName: string;
userId?: string | null;
};

export type InvokeFunctionResponse<ResultT = any> = {
Expand All @@ -37,97 +50,103 @@ export type InvokeFunctionResponse<ResultT = any> = {
};

export type FunctionEvent<
DataT = Record<string, any>,
ExtendObjectT = Record<string, any>
DataT = AnyObject,
ExtendObjectT = AnyObject
> = {
data: DataT;
body: string;
headers: Record<string, string | undefined>;
headers: HttpHeaders;
} & ExtendObjectT;

export type FunctionResponse<
DataT = Record<string, any>,
ExtendObjectT = Record<string, any>,
ErrorT = Record<string, any>
DataT = AnyObject,
ExtendObjectT = AnyObject,
ErrorT = AnyObject
> = Promise<(FunctionResponseObject<DataT, ErrorT> & ExtendObjectT) | void>;

export type FunctionResponseObject<
DataT = Record<string, any>,
ErrorT = Record<string, any>
DataT = AnyObject,
ErrorT = AnyObject
> = {
data?: DataT;
errors?: ErrorT[];
};

export type TriggerResponse<DataT = Record<string, any>> =
FunctionResponseObject<DataT>;

export type BeforeCreateTriggerFunctionEvent<
DataT = Record<string, any>,
ExtendObjectT = Record<string, any>
DataT = AnyObject,
ExtendObjectT = AnyObject
> = {
data: DataT;
headers: Record<string, string | undefined>;
headers: HttpHeaders;
} & ExtendObjectT;

export type BeforeUpdateTriggerFunctionEvent<
DataT = Record<string, any>,
FilterT = Record<string, any>,
OriginalObjectT = Record<string, any>,
ExtendObjectT = Record<string, any>
DataT = AnyObject,
OriginalObjectT = AnyObject,
FilterT = AnyObject,
ExtendObjectT = AnyObject
> = {
data: DataT;
filter: FilterT;
originalObject: OriginalObjectT & { id: string };
headers: Record<string, string | undefined>;
filter?: FilterT;
force?: boolean;
destroyDetached?: boolean;
originalObject: OriginalObjectT & IDBObject;
headers: HttpHeaders;
} & ExtendObjectT;

export type BeforeDeleteTriggerFunctionEvent<
FilterT = Record<string, any>,
OriginalObjectT = Record<string, any>,
ExtendObjectT = Record<string, any>
OriginalObjectT = AnyObject,
FilterT = AnyObject,
ExtendObjectT = AnyObject
> = {
filter: FilterT;
originalObject: OriginalObjectT & { id: string };
headers: Record<string, string | undefined>;
force?: boolean;
destroyDeleted?: boolean;
originalObject: OriginalObjectT & IDBObject;
headers: HttpHeaders;
} & ExtendObjectT;

export type AfterCreateTriggerFunctionEvent<
DataT = Record<string, any>,
OriginalDataT = Record<string, any>,
ExtendObjectT = Record<string, any>
DataT = AnyObject,
OriginalDataT = AnyObject,
ExtendObjectT = AnyObject
> = {
data: DataT & { id: string };
data: DataT & IDBObject;
originalData: OriginalDataT;
body: string;
headers: Record<string, string | undefined>;
headers: HttpHeaders;
} & ExtendObjectT;

export type AfterUpdateTriggerFunctionEvent<
DataT = Record<string, any>,
OriginalDataT = Record<string, any>,
OriginalObjectT = Record<string, any>,
ExtendObjectT = Record<string, any>
DataT = AnyObject,
OriginalDataT = AnyObject,
OriginalObjectT = AnyObject,
ExtendObjectT = AnyObject
> = {
data: DataT & { id: string };
data: DataT & IDBObject;
originalData: OriginalDataT;
originalObject: OriginalObjectT & { id: string };
headers: Record<string, string | undefined>;
originalObject: OriginalObjectT & IDBObject;
headers: HttpHeaders;
} & ExtendObjectT;

export type AfterDeleteTriggerFunctionEvent<
DataT = Record<string, any>,
OriginalDataT = Record<string, any>,
OriginalObjectT = Record<string, any>,
ExtendObjectT = Record<string, any>
DataT = AnyObject,
OriginalDataT = AnyObject,
OriginalObjectT = AnyObject,
ExtendObjectT = AnyObject
> = {
data: DataT & { id: string };
originalData: OriginalDataT;
originalObject: OriginalObjectT & { id: string };
headers: Record<string, string | undefined>;
data: DataT & IDBObject;
originalObject: OriginalObjectT & IDBObject;
headers: HttpHeaders;
} & ExtendObjectT;

export type WebhookFunctionEvent<T extends string = string> = {
body: string;
headers: HttpHeaders;
pathParameters?: Record<T, string | undefined>;
};

export type WebhookResponse = {
statusCode: number;
headers?: HttpHeaders;
body?: string;
};
23 changes: 9 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
# yarn lockfile v1


"@graphql-typed-document-node/core@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@graphql-typed-document-node/core/-/core-3.1.0.tgz#0eee6373e11418bfe0b5638f654df7a4ca6a3950"
integrity sha512-wYn6r8zVZyQJ6rQaALBEln5B1pzxb9shV5Ef97kTvn6yVGrqyXVnDqnU24MXnFubR+rZjBY9NWuxX3FB2sTsjg==

graphql@^15.3.0:
version "15.3.0"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.3.0.tgz#3ad2b0caab0d110e3be4a5a9b2aa281e362b5278"
integrity sha512-GTCJtzJmkFLWRfFJuoo9RWWa/FfamUHgiFosxi/X1Ani4AVWbeyBenZTNX6dM+7WSbbFfTo/25eh0LLkwHMw2w==

typescript@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.3.tgz#153bbd468ef07725c1df9c77e8b453f8d36abba5"
integrity sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==
"@graphql-typed-document-node/core@^3.2.0":
version "3.2.0"
resolved "https://registry.yarnpkg.com/@graphql-typed-document-node/core/-/core-3.2.0.tgz#5f3d96ec6b2354ad6d8a28bf216a1d97b5426861"
integrity sha512-mB9oAsNCm9aM3/SOv4YtBMqZbYj10R7dkq8byBqxGY/ncFwhf2oQzMV+LCRlWoDSEBJ3COiR1yeDvMtsoOsuFQ==

graphql@^15.8.0:
version "15.8.0"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.8.0.tgz#33410e96b012fa3bdb1091cc99a94769db212b38"
integrity sha512-5gghUc24tP9HRznNpV2+FIoq3xKkj5dTQqf4v0CpdPbFVwFkWoxOM+o+2OC9ZSvjEMTjfmG9QT+gcvggTwW1zw==

0 comments on commit 98f2459

Please sign in to comment.