The ReAPI External Library Template is a ready-to-use project designed to empower QA teams by enabling developers to register global utility functions, custom assertion functions, value functions, and API hooks.
With this template, developers can write code just like a standard Node.js project, leveraging any browser-compatible dependencies.
Once the project is built and deployed, it needs to be registered in the ReAPI platform to be accessible. Read below for a step-by-step guide on how to get started.
Before diving into implementation, familiarize yourself with these essential files:
-
src/index.ts
- Central export file
- Manages global type declarations and exports
- Defines the structure of your library's public API
-
src/_support/global.d.ts
- Contains core type declarations
- Defines global interfaces and types
- Essential for TypeScript integration
-
rollup.config.js
- Configures bundle generation
- Defines your library's global namespace (e.g.,
$$CustomLib
) - Manages build optimization settings
-
dts.config.json
- Controls TypeScript declaration bundling
- Manages type declaration dependencies
- Configures type definition output
-
package.json
- Defines package name and version
- Manages dependencies
- Controls build and test scripts
The build process generates two essential output files in the dist
directory:
bundle.umd.js
: The bundled JavaScript code that includes all dependenciesbundle.d.ts
: The TypeScript declaration file that includes all type dependencies
These two files are required to deploy your library to the cloud and register it with ReAPI.
Important: Before writing any function code, please read the 'Writing Functions Compatible with ReAPI Platform' (FUNCTIONS.md) document to ensure your functions will work correctly with the ReAPI platform.
The bundle name must be unique across your ReAPI environment. In this template, we use $$CustomLib
as the global namespace (you can replace this with your own library name, e.g., $$MyAwesomeLib
). This name is used consistently in:
rollup.config.js
for bundle configurationindex.ts
for global type declarations
Always follow this pattern when exposing your APIs src/index.ts
:
declare global {
const $$CustomLib: {
YourClass: typeof YourClass;
yourFunction: typeof yourFunction;
};
}
export { YourClass, yourFunction };
This pattern ensures both:
- Type information is available in ReAPI's web editor
- Functions/classes are accessible globally in ReAPI scripts via
$$CustomLib.YourClass
When using third-party libraries, ensure they are browser-compatible. ReAPI's web executor cannot access Node.js-specific APIs. Common examples of compatible libraries include:
- CryptoJS
- Moment.js
- Browser-compatible portions of utility libraries
The project uses dts-bundle-generator
to bundle TypeScript declarations. Key configuration in dts.config.json
:
{
"libraries": {
"inlinedLibraries": ["@turf/helpers", "geojson"]
},
"output": {
"inlineDeclareGlobals": true
}
}
This configuration:
- Bundles type definitions from dependencies into your final
bundle.d.ts
- Ensures proper type declaration tree-shaking
- Important: The
inlinedLibraries
array must include any packages whose type definitions you want to be included in your final bundle. For example:- If your library depends on turf.js, you need to include
"@turf/helpers"
and"geojson"
as they contain the required type definitions - Without listing dependencies here, their type definitions won't be available in your bundled
bundle.d.ts
file
- If your library depends on turf.js, you need to include
- Clone this template
- Update the bundle name (
$$CustomLib
) to your unique identifier - Update the
name
field inpackage.json
to match your organization (e.g.,@your-org/custom-lib
) - Add your code in
src/
- Build using:
npm install
npm run build
The built library will be available in the dist/
directory:
bundle.js
: Your bundled librarybundle.d.ts
: TypeScript declarations
- Publish your library to npm:
npm publish
-
After publishing, wait a few minutes for the package to be available on CDN providers like unpkg.
-
Register or update the library in ReAPI:
- Navigate to ReAPI's external library management UI
- Add/update the library with specific version CDN URLs for both JS and TypeScript definitions:
JS: https://unpkg.com/[email protected]/dist/bundle.js Types: https://unpkg.com/[email protected]/dist/bundle.d.ts
Note: Avoid using 'latest' in CDN URLs. Always specify exact version numbers for stability.
-
Enable the library in ReAPI's UI.
-
Important: You might need to reload the ReAPI web page to ensure the new library URLs take effect.
-
Your library is now ready to use in ReAPI scripts!
After deploying your library, you can use it in ReAPI scripts:
// Your library is available globally
const result = $$CustomLib.yourFunction();
const value = $$CustomLib.yourClass.someFunction();
Your external library can be utilized in ReAPI's test components:
- Custom Assertion Functions: Create custom assertions using your library's validation logic
- Value Generators/Transformers: Generate test data or transform API responses
- API Hooks: Enhance request/response handling in pre and post hooks
You can develop your library locally with your preferred IDE and AI coding assistants:
- Use VS Code, WebStorm, or any TypeScript-capable IDE
- Set up your favorite AI assistant (e.g., GitHub Copilot, Codeium, TabNine)
- Leverage TypeScript for better code completion and error detection
- Test your code locally before publishing
Always thoroughly test your library before publishing:
// src/__tests__/yourFunction.test.ts
describe("yourFunction", () => {
it("should work as expected", () => {
const result = yourFunction();
expect(result).toBe(expectedValue);
});
});
Run tests with:
npm test
While dts-bundle-generator
works well for most cases, you might encounter scenarios where manual type definition is necessary:
- Complex type hierarchies might not bundle correctly
- Some third-party library types might be incompatible
- Custom type augmentations might need manual handling
In such cases, consider maintaining a manual bundle.d.ts
:
// manually maintained bundle.d.ts
declare global {
const $$CustomLib: {
// Manually specify your types here
YourClass: {
new (): {
someMethod(): void;
};
};
yourFunction(): string;
};
}
export {};