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

Cleanup OpenFgaApi code base #127

Open
rhamzeh opened this issue Jul 17, 2024 · 3 comments
Open

Cleanup OpenFgaApi code base #127

rhamzeh opened this issue Jul 17, 2024 · 3 comments
Labels
breaking change enhancement New feature or request

Comments

@rhamzeh
Copy link
Member

rhamzeh commented Jul 17, 2024

Please do not report security vulnerabilities here. See the Responsible Disclosure Program.

By submitting an issue to this repository, you agree to the terms within the OpenFGA Code of Conduct.

Describe the problem you'd like to have solved

The code base in https://github.com/openfga/js-sdk/blob/main/api.ts is too convoluted, it has many unnecessary abstractions and lots of repeated code that can be cleaned up.

@Siddhant-K-code
Copy link
Contributor

Siddhant-K-code commented Jan 7, 2025

Hi @rhamzeh, I've looked into the cleanup task for api.ts and found that this file is auto-generated by OpenAPI Generator from our API specification. So, not sure how we can clean up that directly as it will be regenerated again.

I am not sure, where to find our OpenAPI Generator.

@ewanharris
Copy link
Member

👋🏻 Our generation pieces (SDK templates, scripts etc.) live in the openfga/sdk-generator repo.

The code being referred to is specifically in the js template in the apiInner.mustache file.

I'm not sure if @rhamzeh is thinking the same as myself, but I think we can potentially do away with everything in there except the OpenFgaApi class, folding things like parameter assertions (template), request building (template), and request/telemetry creation (template) into the class methods directly as opposed to how they're currently spread across different methods

@rhamzeh
Copy link
Member Author

rhamzeh commented Jan 9, 2025

To elaborate on what @ewanharris said:

Some concerns of mine:

  • We have the methods in the OpenFgaApi, e.g.

    js-sdk/api.ts

    Lines 1216 to 1218 in 006b5c4

    public check(storeId: string, body: CheckRequest, options?: any): Promise<CallResult<CheckResponse>> {
    return OpenFgaApiFp(this.configuration, this.credentials).check(storeId, body, options).then((request) => request(this.axios));
    }
    that just call other implementations, but no one uses those, the implementation should be moved here and the methods dropped.
  • Many methods have duplicate code. e.g. this section or something similar

    js-sdk/api.ts

    Lines 587 to 623 in 006b5c4

    // verify required parameter 'storeId' is not null or undefined
    assertParamExists("readChanges", "storeId", storeId);
    const localVarPath = "/stores/{store_id}/changes"
    .replace(`{${"store_id"}}`, encodeURIComponent(String(storeId)));
    // use dummy base URL string because the URL constructor only accepts absolute URLs.
    const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
    let baseOptions;
    if (configuration) {
    baseOptions = configuration.baseOptions;
    }
    const localVarRequestOptions = { method: "GET", ...baseOptions, ...options};
    const localVarHeaderParameter = {} as any;
    const localVarQueryParameter = {} as any;
    if (type !== undefined) {
    localVarQueryParameter["type"] = type;
    }
    if (pageSize !== undefined) {
    localVarQueryParameter["page_size"] = pageSize;
    }
    if (continuationToken !== undefined) {
    localVarQueryParameter["continuation_token"] = continuationToken;
    }
    if (startTime !== undefined) {
    localVarQueryParameter["start_time"] = (startTime as any instanceof Date) ?
    (startTime as any).toISOString() :
    startTime;
    }
    setSearchParams(localVarUrlObj, localVarQueryParameter, options.query);
    localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
    is implemented for each method. We should have a common function that takes any specifics and returns the relevant data - we can pass in the required attributes, what the method is, etc..

The template for this file currently lives in https://github.com/openfga/sdk-generator/blob/main/config/clients/js/template/apiInner.mustache

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

3 participants