diff --git a/.npmignore b/.npmignore index 60f1a29..9dcbeb8 100644 --- a/.npmignore +++ b/.npmignore @@ -9,4 +9,5 @@ !/README_EN.md !/package.json !/cjs/**/* +!/types/**/* !/esm/**/* \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 574fec2..280b4db 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.7", "license": "MIT", "devDependencies": { - "@types/node": ">=8.1.0", + "@types/node": "^20.14.9", "husky": "^9.0.11", "typescript": "^5.5.3" }, diff --git a/package.json b/package.json index c92f187..10b8b30 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "url": "https://github.com/redocmx/client-node/issues" }, "devDependencies": { - "@types/node": ">=8.1.0", + "@types/node": "^20.14.9", "husky": "^9.0.11", "typescript": "^5.5.3" } diff --git a/src/file.ts b/src/file.ts index 452ab15..ed953ca 100644 --- a/src/file.ts +++ b/src/file.ts @@ -19,6 +19,11 @@ export default class File { return this } + fromBuffer(fileBuffer: Buffer) { + this.fileBuffer = fileBuffer; + return this + } + fromString(fileContent: string) { this.fileContent = fileContent; return this diff --git a/src/redoc.cjs.ts b/src/redoc.cjs.ts index 831690c..23c01d7 100644 --- a/src/redoc.cjs.ts +++ b/src/redoc.cjs.ts @@ -1,4 +1,4 @@ -import { Redoc } from "./index"; +import { Redoc } from "./redoc"; module.exports = Redoc; diff --git a/src/redoc.esm.ts b/src/redoc.esm.ts index 2846d9c..88748a1 100644 --- a/src/redoc.esm.ts +++ b/src/redoc.esm.ts @@ -1,3 +1,3 @@ -import { Redoc } from "./index.js"; +import { Redoc } from "./redoc.js"; export default Redoc \ No newline at end of file diff --git a/src/index.ts b/src/redoc.ts similarity index 100% rename from src/index.ts rename to src/redoc.ts diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json index 688a39e..7fd347a 100644 --- a/tsconfig.cjs.json +++ b/tsconfig.cjs.json @@ -10,7 +10,7 @@ "strict": true, "strictFunctionTypes": true, "types": [ "node" ], - "esModuleInterop": true, + "esModuleInterop": true }, "include": ["./src/**/*"], "exclude": ["./src/redoc.esm.ts"] diff --git a/types/Addenda.d.ts b/types/Addenda.d.ts new file mode 100644 index 0000000..d2b04e7 --- /dev/null +++ b/types/Addenda.d.ts @@ -0,0 +1,20 @@ +/// + +declare module 'redocmx' { + namespace Redoc { + + namespace Addenda { + type ReplaceOptions = { + [key: string]: string; + } + } + + class Addenda extends File { + constructor(); + + replaceValues(content: string, options?: Addenda.ReplaceOptions | null): string; + + getFileContent(replaceValues: Addenda.ReplaceOptions): Promise; + } + } +} \ No newline at end of file diff --git a/types/Assets.d.ts b/types/Assets.d.ts new file mode 100644 index 0000000..89d0d30 --- /dev/null +++ b/types/Assets.d.ts @@ -0,0 +1,68 @@ +/// +/// + +declare module 'redocmx' { + namespace Redoc { + + namespace Assets { + + type AssetType = 'folder' | 'image'; + + interface Asset { + path: string; + created_at: string, + updated_at: string, + type: AssetType, + name: string, + metadata: object, + tags: Array, + parent_id: string, + } + + interface Folder extends Asset { + folder_items_count: number, + } + + interface Image extends Asset { + image_type: string, + image_size: number, + url: string, + url_small: string + } + + namespace Service { + + interface BasicParams { + path: string; + } + + interface FetchParams extends BasicParams { + path: string; + options?: Network.Request.Options; + } + + interface PutParams extends BasicParams { + file: File.Result; + } + + interface DeleteParams extends BasicParams {} + + } + } + + class Assets { + service: Service; + + constructor(service: Service); + + get(path: string): Promise; + + delete(path: string): Promise; + + list(path: string, options: Network.Request.Pagination.Options): Promise>; + + put(path: string, source: string | Buffer): Promise; + } + + } +} \ No newline at end of file diff --git a/types/Cfdi.d.ts b/types/Cfdi.d.ts new file mode 100644 index 0000000..4bcb9ed --- /dev/null +++ b/types/Cfdi.d.ts @@ -0,0 +1,35 @@ +/// +/// +/// +/// + +declare module 'redocmx' { + namespace Redoc { + + namespace Cfdi { + type Metadata = { + [key: string]: string; + } + + namespace Convert { + interface Params { + file: File.Result; + payload?: Pdf.Payload; + } + } + } + + class Cfdi extends File { + pdf: Pdf | null; + service: Service; + addenda: Addenda | null; + addendaReplaceValues: Addenda.ReplaceOptions | null; + + constructor(service: Service); + + setAddenda(addenda: Addenda, replaceValues?: any): void; + + toPdf(payload?: Pdf.Payload): Promise; + } + } +} diff --git a/types/File.d.ts b/types/File.d.ts new file mode 100644 index 0000000..eae894f --- /dev/null +++ b/types/File.d.ts @@ -0,0 +1,36 @@ +/// + +declare module 'redocmx' { + namespace Redoc { + + namespace File { + interface Result { + content: string | Buffer; + type: string; + } + } + + class File { + filePath: string | null; + fileBuffer: Buffer | null; + fileContent: string | null; + + constructor(); + + fromFile(filePath: string): this; + + fromBuffer(fileBuffer: Buffer): this; + + fromString(fileContent: string): this; + + getFile(): Promise<{ + content: string; + type: string; + } | { + content: Buffer; + type: string; + }>; + } + + } +} \ No newline at end of file diff --git a/types/Global.d.ts b/types/Global.d.ts new file mode 100644 index 0000000..f300b7f --- /dev/null +++ b/types/Global.d.ts @@ -0,0 +1,37 @@ +/// + +declare module 'redocmx' { + namespace Redoc { + namespace Network { + namespace Request { + + interface Options { + headers?: { [key: string]: any }; + body?: { [key: string]: any } | FormData; + params?: { [key: string]: any }; + isForm?: boolean; + isBufferResponse?: boolean; + } + + namespace Pagination { + + interface Options { + nextToken?: string; + limit?: number; + } + + interface Response { + data: Array; + pagination: { + limit: number; + has_more: boolean; + next_token: string | null + } + + } + + } + } + } + } +} \ No newline at end of file diff --git a/types/Pdf.d.ts b/types/Pdf.d.ts new file mode 100644 index 0000000..45fd580 --- /dev/null +++ b/types/Pdf.d.ts @@ -0,0 +1,44 @@ +/// +/// + +declare module 'redocmx' { + namespace Redoc { + + namespace Pdf { + + interface ConstructorParams { + buffer: Buffer; + transactionId: string; + totalPages: number; + totalTime: number; + metadata: Cfdi.Metadata + } + + interface Payload { + style_pdf?: string; + format?: string; + addenda?: string; + } + } + + class Pdf { + buffer: Buffer; + transactionId: string; + totalPages: number; + totalTime: number; + metadata: Cfdi.Metadata; + + constructor({ buffer, transactionId, totalPages, totalTime, metadata }: Pdf.ConstructorParams); + + toBuffer(): Buffer; + + getTransactionId(): string; + + getTotalPages(): number; + + getTotalTimeMs(): number; + + getMetadata(): Cfdi.Metadata; + } + } +} \ No newline at end of file diff --git a/types/Service.d.ts b/types/Service.d.ts new file mode 100644 index 0000000..97ed1ce --- /dev/null +++ b/types/Service.d.ts @@ -0,0 +1,39 @@ +/// +/// +/// +/// + +declare module 'redocmx' { + namespace Redoc { + + namespace Service { + interface ConstructorParams { + apiKey: string; + apiUrl?: string; + } + } + + class Service { + apiKey?: string; + apiUrl?: string; + + constructor(config?: string | Service.ConstructorParams); + + cfdisConvert({ file, payload }: Cfdi.Convert.Params): Promise; + + fetchAssets({ path, options }: Assets.Service.FetchParams): Promise>; + + putAsset({ path, file }: Assets.Service.PutParams): Promise; + + deleteAsset({ path }: Assets.Service.BasicParams): Promise; + + _request(endpoint: string, method: string, config?: Network.Request.Options): Promise<{ + code: number; + data: any; + headers: Headers; + }>; + } + + } +} + diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..71031a5 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,31 @@ +/// +/// +/// +/// +/// +/// + +declare module 'redocmx' { + // Added to in other modules, referenced above. + export namespace Redoc { + interface ConstructorParams { + apiKey: string; + apiUrl?: string; + } + } + + export class Redoc { + static Redoc: typeof Redoc; + + constructor(config: string | Redoc.ConstructorParams); + + apiKey?: string; + service?: Redoc.Service; + + get cfdi(): Redoc.Cfdi; + get addenda(): Redoc.Addenda; + get assets(): Redoc.Assets; + } + + export default Redoc; +} \ No newline at end of file