Skip to content

Commit

Permalink
feat: add optional extra props for custom API call
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSelene committed Dec 4, 2024
1 parent cf322b9 commit 6d58717
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 42 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

102 changes: 68 additions & 34 deletions packages/pieces/community/common/src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,71 @@
import { OAuth2PropertyValue, PieceAuthProperty, Property, StaticDropdownProperty, createAction } from "@activepieces/pieces-framework";
import { HttpError, HttpHeaders, HttpMethod, HttpRequest, QueryParams, httpClient } from "../http";
import { assertNotNullOrUndefined } from "@activepieces/shared";
import {
OAuth2PropertyValue,
PieceAuthProperty,
Property,
StaticDropdownProperty,
createAction,
StaticPropsValue,
InputPropertyMap,
} from '@activepieces/pieces-framework';
import {
HttpError,
HttpHeaders,
HttpMethod,
HttpRequest,
QueryParams,
httpClient,
} from '../http';
import { assertNotNullOrUndefined } from '@activepieces/shared';

export const getAccessTokenOrThrow = (auth: OAuth2PropertyValue | undefined): string => {
export const getAccessTokenOrThrow = (
auth: OAuth2PropertyValue | undefined
): string => {
const accessToken = auth?.access_token;

if (accessToken === undefined) {
throw new Error("Invalid bearer token");
throw new Error('Invalid bearer token');
}

return accessToken;
};

export function createCustomApiCallAction({ auth, baseUrl, authMapping, description, displayName, name, props }: {
auth?: PieceAuthProperty,
baseUrl: (auth?: unknown) => string,
authMapping?: (auth: unknown) => Promise<HttpHeaders>,
export function createCustomApiCallAction({
auth,
baseUrl,
authMapping,
description,
displayName,
name,
props,
extraProps,
}: {
auth?: PieceAuthProperty;
baseUrl: (auth?: unknown) => string;
authMapping?: (
auth: unknown,
propsValue: StaticPropsValue<any>
) => Promise<HttpHeaders>;
// add description as a parameter that can be null
description?: string | null,
displayName?: string | null,
name?: string | null,
description?: string | null;
displayName?: string | null;
name?: string | null;
props?: {
url?: Partial<ReturnType<typeof Property.ShortText>>,
method?: Partial<StaticDropdownProperty<HttpMethod, boolean>>,
headers?: Partial<ReturnType<typeof Property.Object>>,
queryParams?: Partial<ReturnType<typeof Property.Object>>,
body?: Partial<ReturnType<typeof Property.Json>>,
failsafe?: Partial<ReturnType<typeof Property.Checkbox>>,
timeout?: Partial<ReturnType<typeof Property.Number>>,
}
url?: Partial<ReturnType<typeof Property.ShortText>>;
method?: Partial<StaticDropdownProperty<HttpMethod, boolean>>;
headers?: Partial<ReturnType<typeof Property.Object>>;
queryParams?: Partial<ReturnType<typeof Property.Object>>;
body?: Partial<ReturnType<typeof Property.Json>>;
failsafe?: Partial<ReturnType<typeof Property.Checkbox>>;
timeout?: Partial<ReturnType<typeof Property.Number>>;
};
extraProps?: InputPropertyMap;
}) {
return createAction({
name: name ? name : 'custom_api_call',
displayName: displayName ? displayName : 'Custom API Call',
description: description ? description : 'Make a custom API call to a specific endpoint',
description: description
? description
: 'Make a custom API call to a specific endpoint',
auth: auth ? auth : undefined,
requireAuth: auth ? true : false,
props: {
Expand All @@ -49,26 +81,27 @@ export function createCustomApiCallAction({ auth, baseUrl, authMapping, descript
required: true,
defaultValue: baseUrl(auth),
...(props?.url ?? {}),
})
}
}
}),
};
},
}),
method: Property.StaticDropdown({
displayName: 'Method',
required: true,
options: {
options: Object.values(HttpMethod).map(v => {
options: Object.values(HttpMethod).map((v) => {
return {
label: v,
value: v,
}
})
};
}),
},
...(props?.method ?? {}),
}),
headers: Property.Object({
displayName: 'Headers',
description: 'Authorization headers are injected automatically from your connection.',
description:
'Authorization headers are injected automatically from your connection.',
required: true,
...(props?.headers ?? {}),
}),
Expand All @@ -92,6 +125,7 @@ export function createCustomApiCallAction({ auth, baseUrl, authMapping, descript
required: false,
...(props?.timeout ?? {}),
}),
...extraProps,
},

run: async (context) => {
Expand All @@ -103,12 +137,12 @@ export function createCustomApiCallAction({ auth, baseUrl, authMapping, descript

let headersValue = headers as HttpHeaders;
if (authMapping) {
const headers = await authMapping(context.auth)
const headers = await authMapping(context.auth, context.propsValue);
if (headers) {
headersValue = {
...headersValue,
...headers
}
...headers,
};
}
}

Expand All @@ -128,10 +162,10 @@ export function createCustomApiCallAction({ auth, baseUrl, authMapping, descript
return await httpClient.sendRequest(request);
} catch (error) {
if (failsafe) {
return (error as HttpError).errorMessage()
return (error as HttpError).errorMessage();
}
throw error;
}
}
})
},
});
}
2 changes: 1 addition & 1 deletion packages/pieces/community/slack/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-slack",
"version": "0.7.8"
"version": "0.7.9"
}
29 changes: 24 additions & 5 deletions packages/pieces/community/slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
OAuth2PropertyValue,
PieceAuth,
createPiece,
Property,
} from '@activepieces/pieces-framework';
import { PieceCategory } from '@activepieces/shared';
import crypto from 'node:crypto';
Expand Down Expand Up @@ -60,7 +61,7 @@ export const slackAuth = PieceAuth.OAuth2({
export const slack = createPiece({
displayName: 'Slack',
description: 'Channel-based messaging platform',
minimumSupportedRelease: '0.30.0',
minimumSupportedRelease: '0.37.3',
logoUrl: 'https://cdn.activepieces.com/pieces/slack.png',
categories: [PieceCategory.COMMUNICATION],
auth: slackAuth,
Expand Down Expand Up @@ -124,10 +125,28 @@ export const slack = createPiece({
return 'https://slack.com/api';
},
auth: slackAuth,
authMapping: async (auth) => {
return {
Authorization: `Bearer ${(auth as OAuth2PropertyValue).access_token}`,
};
authMapping: async (auth, propsValue) => {
if (propsValue.useUserToken) {
return {
Authorization: `Bearer ${
(auth as OAuth2PropertyValue).data['authed_user']?.access_token
}`,
};
} else {
return {
Authorization: `Bearer ${
(auth as OAuth2PropertyValue).access_token
}`,
};
}
},
extraProps: {
useUserToken: Property.Checkbox({
displayName: 'Use user token',
description: 'Use user token instead of bot token',
required: true,
defaultValue: false,
}),
},
}),
],
Expand Down

0 comments on commit 6d58717

Please sign in to comment.