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

Add GUID support for route params #1116

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/abstractions/src/requestInformation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Guid } from 'guid-typescript';
import { trace } from "@opentelemetry/api";
import { StdUriTemplate } from "@std-uritemplate/std-uritemplate";

Expand Down Expand Up @@ -252,6 +253,8 @@ export class RequestInformation implements RequestInformationSetContent {
writer.writeTimeOnlyValue(undefined, value as any as TimeOnly);
} else if (value instanceof Duration) {
writer.writeDurationValue(undefined, value as any as Duration);
} else if (value instanceof Guid) {
Copy link
Member

Choose a reason for hiding this comment

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

This if statement is for the request body, it's good to fix the issue here as well as people will eventually run into it, but it's not going to address the initial issue.

Copy link
Contributor

@yasaichi yasaichi Apr 3, 2024

Choose a reason for hiding this comment

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

@illunix
I think there are 2 possible ways to convert a Guid value in path parameters to a string:

Inside RequestInformation.URL

This getter is called just before StdUriTemplate.expand. Using the method seems better to me.

public get URL(): string {
  const rawUrl = this.pathParameters[
    RequestInformation.raw_url_key
  ] as string;
  if (this.uri) {
    ...
  } else {
    const data = {} as { [key: string]: unknown };
    for (const key in this.queryParameters) {
      if (this.queryParameters[key] !== null && this.queryParameters[key] !== undefined) {
        data[key] = this.queryParameters[key];
      }
    }
    for (const key in this.pathParameters) {
      if (this.pathParameters[key]) {
        data[key] = this.pathParameters[key]; // <- here
      }
    }
    return StdUriTemplate.expand(this.urlTemplate, data);
  }
}

Inside apiClientProxifier

This function is called when you write like apiClient.foos.byId(fooId).

export function apiClientProxifier<T extends object>(
  requestAdapter: RequestAdapter,
  pathParameters: Record<string, unknown>,
  navigationMetadata?: Record<string, NavigationMetadata>,
  requestsMetadata?: RequestsMetadata,
): T {
  if (!requestAdapter) throw new Error("requestAdapter cannot be undefined");
  if (!pathParameters) throw new Error("pathParameters cannot be undefined");
  return new Proxy({} as T, {
    get(target, property) {
      ...
      if (navigationMetadata) {
        const navigationCandidate = navigationMetadata[name];
        if (navigationCandidate) {
          ...
          return (...argArray: any[]) => {
            // navigation method like indexers or multiple path parameters
            const downWardPathParameters = getPathParameters(pathParameters);
            if (
              navigationCandidate.pathParametersMappings &&
              navigationCandidate.pathParametersMappings.length > 0
            ) {
              for (let i = 0; i < argArray.length; i++) {
                const element = argArray[i];
                downWardPathParameters[
                  navigationCandidate.pathParametersMappings[i]
                ] = element; // <- here
              }
            }
            return apiClientProxifier(
              requestAdapter,
              downWardPathParameters,
              navigationCandidate.navigationMetadata,
              navigationCandidate.requestsMetadata,
            );
          };
        }
        throw new Error(
          `couldn't find navigation property ${name} data: ${JSON.stringify(
            navigationMetadata,
          )}`,
        );
      }
    },
  });
}

I hope that this will help you. Thanks

writer.writeGuidValue(undefined, value as any as Guid);
} else if (valueType === "number") {
writer.writeNumberValue(undefined, value as any as number);
} else if (Array.isArray(value)) {
Expand Down
Loading