Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IN-4121 - async hooks / api client #7155

Merged
merged 12 commits into from
Jun 6, 2024
Merged

IN-4121 - async hooks / api client #7155

merged 12 commits into from
Jun 6, 2024

Conversation

Olbix
Copy link
Contributor

@Olbix Olbix commented May 16, 2024

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSDoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Copy link

changeset-bot bot commented May 16, 2024

⚠️ No Changeset found

Latest commit: 7b78579

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

}

next();
getApiFunction(apiClientInstance, functionName, extensionName)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit pick but I'd like to have async/await syntax in here as well (easier debugging imho), but you'd need to use this lib: https://www.npmjs.com/package/express-async-handler

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make onCreate async in packages/middleware/__tests__/integration/bootstrap/server.ts to validate async behaviour?

krystian wlodarski added 2 commits May 17, 2024 01:46
# Conflicts:
#	packages/middleware/src/apiClientFactory/index.ts
#	packages/middleware/src/handlers/prepareApiFunction/getApiFunction.ts
#	packages/middleware/src/handlers/prepareApiFunction/index.ts
Copy link
Collaborator

@WojtekTheWebDev WojtekTheWebDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, missing the integration tests to validate full scenario with extensions.

Comment on lines +6 to +7
): (req, res, next) => Promise<any> {
return async (req, res, next) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this hack in here ;p I'd go with https://www.npmjs.com/package/express-async-handler and avoid defining the return type, then express will validate if it returns what's expected.

Suggested change
): (req, res, next) => Promise<any> {
return async (req, res, next) => {
) {
return asyncHandler(async (req, res, next) => { ... });

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also try without the lib and without the Promise in return type

);
.reduce(async (configSoFar, extension) => {
const resolvedConfig = await configSoFar;
return extension.beforeCreate({ configuration: resolvedConfig });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await on lifecycles seems fine but I think you missed async behaviour on hooks like beforeCreate, afterCreate, beforeCall and afterCall

# Conflicts:
#	packages/middleware/src/apiClientFactory/index.ts
#	packages/middleware/src/handlers/prepareApiFunction/getApiFunction.ts
#	packages/middleware/src/handlers/prepareApiFunction/index.ts
const { settings } = createApiClient(clientConfig);
const { settings } = await createApiClient(clientConfig);

await settings.client;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

@@ -76,11 +78,11 @@ describe("apiClientFactory", () => {
};

const { createApiClient } = apiClientFactory<any, any>(params as any);
const extensions = (createApiClient as any)._predefinedExtensions;
const extensions = ((await createApiClient) as any)._predefinedExtensions;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd split that

Suggested change
const extensions = ((await createApiClient) as any)._predefinedExtensions;
const apiClient = (await createApiClient) as any;
const extensions = apiClient._predefinedExtensions;

createApiClient.bind({ middleware: { req: null, res: null, extensions } })(
{}
);
await createApiClient.bind({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're calling this function immediately anyway, perhaps you could replace .bind() with .call()?

@@ -106,7 +108,7 @@ describe("apiClientFactory", () => {
const { createApiClient } = apiClientFactory<any, any>(params as any);
const extensions = (createApiClient as any)._predefinedExtensions;

const apiWithContext: any = createApiClient.bind({
const apiWithContext: any = await createApiClient.bind({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're calling this function immediately anyway, perhaps you could replace .bind() with .call()?

same here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this suite testing, actually? :P

@WojtekTheWebDev WojtekTheWebDev marked this pull request as ready for review June 6, 2024 11:43
@WojtekTheWebDev WojtekTheWebDev requested a review from a team as a code owner June 6, 2024 11:43
Copy link
Collaborator

@WojtekTheWebDev WojtekTheWebDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved in advance but type casting to any might be redundant

configuration: {},
location: "./__tests__/integration/bootstrap/server",
extensions() {
return [myExtension as any];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why as any?

@Olbix Olbix merged commit e6fd664 into main Jun 6, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants