I am new to TypeScript (TS) - I have the following class in JavaScript (JS) (this needs to be in JavaScript due to the azure imports not playing with TS). I have the following JS file azureSasTokenGenerator.js
import AzureSasTokenConfig from './azureSasTokenConfig';
import * as crypto from 'crypto-js';
const { enc, HmacSHA256 } = crypto;
class AzureSasTokenGenerator {
#accountName = null;
#containerName = null;
#accountAccessKey = null;
#expiryPeriodMins = 60;
#apiVersion = '2020-12-06';
#useIpAddress = false;
#ipAddress = null;
constructor(
accountName,
containerName,
accountAccessKey,
expiryPeriodMins,
apiVersion,
useIpAddress,
ipAddress) {
this.#accountName = accountName;
this.#containerName = containerName;
this.#accountAccessKey = accountAccessKey;
this.#expiryPeriodMins = expiryPeriodMins;
this.#apiVersion = apiVersion;
this.#useIpAddress = useIpAddress;
this.#ipAddress = ipAddress;
}
getUrl(url) {
var token = this.getSasToken();
return `${url}${token}`;
}
getSasToken() {
const config = new AzureSasTokenConfig(
this.#apiVersion,
now.toISOString().split('.')[0] + 'Z',
`${new Date(now.getTime() + this.#expiryPeriodMins * 60 * 1000).toISOString().split('.')[0]}Z`,
'c',
'r',
this.#ipAddress,
`/blob/${this.#accountName}/${this.#containerName}`
);
const signature = this.#createSignature(config);
// ...
return 'some string';
}
#createSignature(config) {
// ...
// Convert signature to UTF-8 and hash.
const plainSignature = JSON.parse(JSON.stringify(stringToSign));
const hash = HmacSHA256(plainSignature, accessKeyUtf);
const signature = enc.Base64.stringify(hash);
// Return the encoded url.
return encodeURIComponent(signature);
}
}
export default AzureSasTokenGenerator;
Now in a unit test for this class called azureSasTokenGenerator.test.ts I am doing
import AzureSasTokenGenerator from '../azureSasTokenGenerator';
describe("SAS Generator Tests", () => {
test("Get a valid SAS token", () => {
expect.assertions(1);
const azureSasGenerator = new AzureSasTokenGenerator("M8", "Container", "biGL0ngKEY", 90, "2018-09-09", false, null);
const azureSasToken = azureSasGenerator.getSasToken();
expect(azureSasToken).toContain("?sv=2018-09-09");
});
});
But this is complaining about the import of the js class. So I have read about declaration files (.d.ts) and I have attempted to create a declaration file with the same name azureSasTokenGenerator.d.ts
declare class AzureSasTokenGenerator {
constructor(
accountName: string,
containerName: string,
accountAccessKey: string,
expiryPeriodMins: number,
apiVersion: string,
useIpAddress: boolean,
ipAddress: string);
getUrl(): string;
getSasToken(): string;
#createSignature(config: any): string;
}
export = AzureSasTokenGenerator;
This has got read of the intellisense error but the test fails to run with following error
***SyntaxError: Cannot use import statement outside a module
1 | import AzureSasTokenGenerator from '../azureSasTokenGenerator'; | ^ 2 | 3 | describe("SAS Generator Tests", () => { 4 | test("Get a valid SAS token", () => { at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14) at Object. (src/azure/test/azureSasTokenGenerator.test.ts:1:1)***
How can I edit my .d.ts file to allow this to be consumed in my test?