grafana / k6-jslib-aws

Javascript Library allowing to interact with AWS resources from k6 scripts
Apache License 2.0
18 stars 29 forks source link

Missing type definition for `https://jslib.k6.io/k6-utils/1.4.0/index.js` import #118

Open fullheart opened 1 month ago

fullheart commented 1 month ago

After upgrading to latest version (0.13.0) to resolve a bug, I got an other problem:

node_modules/k6-jslib-aws/src/internal/secrets-manager.ts:3:24 - error TS2307: Cannot find module 'https://jslib.k6.io/k6-utils/1.4.0/index.js' or its corresponding type declarations.

3 import { uuidv4 } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error in node_modules/k6-jslib-aws/src/internal/secrets-manager.ts:3

Versions: k6-jslib-aws v0.13.0, @types/k6 0.54.0

Steps to reproduce

git clone git@github.com:grafana/k6-jslib-aws.git && cd k6-jslib-aws/ && git checkout v0.13.0
npm install --save-dev @types/k6
npx tsc -t es5 src/internal/secrets-manager.ts
mstoykov commented 1 month ago

This repo doesn't build with tsc directly but with webpack. You can look at the documentation. And webpack specifically doesn't look into https:// imports as they are usually not supported. I am not even certain tsc supports remote modules import. Looking at the error it seems like it doesn't do it well.

btw, what are you trying to do? You don't need to compile the module it is already available in jslib.

fullheart commented 1 month ago

Thanks @mstoykov for you answer.

Our k6 use case We are load testing our AWS SageMaker endpoints (ML models) with k6. Our project is written in TypeScript (using Vite to compile into JavaScript files). We have built a k6 client to invoke a SageMaker endpoint. These components are used from this repo:

import { AWSConfig, SignatureV4 } from "k6-jslib-aws";
import { AWSClient } from "k6-jslib-aws/src/internal/client";
import { HTTPRequest, SignedHTTPRequest } from "k6-jslib-aws/src/internal/http";

With tsc the CI pipeline validates the .ts files.

We work around this problem for now with this patch (created with patch-package):

diff --git a/node_modules/k6-jslib-aws/src/internal/secrets-manager.ts b/node_modules/k6-jslib-aws/src/internal/secrets-manager.ts
index cd5967f..dc9148c 100644
--- a/node_modules/k6-jslib-aws/src/internal/secrets-manager.ts
+++ b/node_modules/k6-jslib-aws/src/internal/secrets-manager.ts
@@ -1,5 +1,6 @@
 import { JSONArray, JSONObject } from 'k6'
 import http, { RefinedResponse, ResponseType } from 'k6/http'
+// @ts-ignore
 import { uuidv4 } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'

 import { AWSClient } from './client'
mstoykov commented 1 month ago

@oleiade do we want to get this in our code as well?

oleiade commented 1 month ago

Just to confirm my understanding, a fix that would cater to the problem would be to add a // @ts-ignore statement on our import statement of k6-utils in jslib-aws. Is that correct @fullheart ?

fullheart commented 1 month ago

Thanks @mstoykov and @oleiade

Yes, adding the // @ts-ignore line would be a solution.

Another solution would be, when you can add a type definition file (d.ts) in the @types/k6 package that contains all function of this utils package. Example:

declare module 'https://jslib.k6.io/k6-utils/1.4.0/index.js' {
    export function uuidv4(): string;
   // TODO: Add all functions of utils package (https://grafana.com/docs/k6/latest/javascript-api/jslib/utils/#utils)
}

I prefer the second solution to having typed utils functions.