chris-armstrong / rescript-aws-sdk-v3-wrapper

ReScript bindings for the AWS SDK V3
MIT License
18 stars 2 forks source link

S3 Client is not configurable as S3.createClient() does not accept any parameter #4

Open superboum opened 2 years ago

superboum commented 2 years ago

S3 Client is not configurable as S3.createClient does not accept any parameter. S3.createClient() is the binding for the S3Client constructor. The JS API accepts an object that is defined by the S3ConfigClient interface but the ReasonAPI does not accept anything in its input.

chris-armstrong commented 2 years ago

This is definitely a limitation with the current implementation. One problem I can forsee is that all the clients combine a number of standard and service-specific properties, and while its easy enough to generate the standard ones as boilerplate, the service specific ones do not seem to appear in the model files.

Patches are welcome if you know how to extend the generator.

superboum commented 2 years ago

Thanks a lot for your answer. Unfortunately I am very new to Rescript so I am mainly in a discovering phase. I opened this issue in part to be sure that I have not missed something ^^. I will keep in mind your suggestion and try to contribute if I can. Thanks for open sourcing this work, even if it does not work for me, it is still impressive :-)

chris-armstrong commented 2 years ago

As a workaround, you could initialize the client in JS or with your own binding and then pass in the command object from this wrapper library. Sorry if I don’t get to this soon.

superboum commented 2 years ago

This is what I have tried indeed, and found another problem.

ListBuckets does not accept any argument on rescript but it seems that it expects an empty javascript object as input {} otherwise it fails with:

<redacted>/node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/validate-bucket-name.js:7
        const { input: { Bucket }, } = args;
                         ^

TypeError: Cannot read properties of undefined (reading 'Bucket')
    at <redacted>/node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/validate-bucket-name.js:7:26
    at <redacted>/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:6:28
    at S3Client.send (<redacted>/node_modules/@aws-sdk/smithy-client/dist-cjs/client.js:20:20)
    at file:///<redacted>/test.mjs:22:4
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

As a workaround, I redefined the module as follow:

module ListBuckets = {
  @module("@aws-sdk/client-s3") @new external new: {..} => AwsSdkV3.S3.ListBuckets.t = "ListBucketsCommand"
  let make = () => Js.Obj.empty() -> new
  @send external send: (s3Client, AwsSdkV3.S3.ListBuckets.t) => Js.Promise.t<AwsSdkV3.S3.ListBuckets.response> = "send"
}

Do you want that I report possible additional issues I encounter in this ticket, or in separate tickets, or you consider that all these bugs fall under the same binding problem?

chris-armstrong commented 2 years ago

This particular case is indeed unusual because ListBuckets operation does not take any parameters. Nonetheless, it appears the AWS SDK requires an empty object parameter because it is applying a generic validation middleware. I’ll need to check the equivalent typescript definition for this command but it seems an empty object is needed for these cases, which may be hard to express cleanly in ReScript.

https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListBuckets.html

chris-armstrong commented 2 years ago

Indeed, an empty input shape is generated in the typescript and made a required parameter of the command. I’ll have to find a way to do this for ReScript to match the behaviour.

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/listbucketscommand.html

chris-armstrong commented 2 years ago

I've pushed a version 2.0.0 that should address the empty constructor issue for the ListBuckets command and others, but I'm not sure what to do with the client-configuration issue just yet, so I'll leave this open.