I have developed a Web API and now I want to streamline my code by creating a library that can be reused in any new project I create that interacts with this API.
My goal is to organize my code efficiently, so I plan to have separate classes for each endpoint structure. For example, if I have multiple User endpoints such as Register and Login, I would consolidate them into a single UserClient class offering both methods:
export default class UserClient
{
register(email: string, password: string)
{
// implementation here
}
login(email: string, password: string, remember: bool)
{
// implementation here
}
}
Here's how I structured my code:
package.json
tsconfig.json
--src
----api
------user-client.ts
----models
------user
--------logindata.ts
My tsconfig.json contains the following configurations:
{
"compilerOptions": {
"declaration": true,
...
}
I also have a package.json file specifying details about the library:
{
"name": "xxx",
"version": "1.0.0",
...
}
As I continue developing my library, I came across several questions:
- Do I need an index.ts file in src? If yes, what should be included in it?
- Should I change the types field to something else in package.json?
Ultimately, my aim is to ensure typings exist for every class within the library so that they can be imported and used seamlessly in other projects like this:
import {UserClient} from "myLib"
import {BooksClient} from "myLib"
However, when I compile the library using tsc
, it does not generate an index.d.ts file (due to the absence of an index.ts) and places the user-client.js and user-client.d.ts directly in the dist folder without maintaining the existing structure. This impedes their usage in other libraries.