Skip to content

Commit

Permalink
feat: improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
David Osorio committed Jul 3, 2024
1 parent 8a9cd4c commit 051559c
Show file tree
Hide file tree
Showing 16 changed files with 321 additions and 5 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
!/README_EN.md
!/package.json
!/cjs/**/*
!/types/**/*
!/esm/**/*
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
5 changes: 5 additions & 0 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/redoc.cjs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Redoc } from "./index";
import { Redoc } from "./redoc";

module.exports = Redoc;

Expand Down
2 changes: 1 addition & 1 deletion src/redoc.esm.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Redoc } from "./index.js";
import { Redoc } from "./redoc.js";

export default Redoc
File renamed without changes.
2 changes: 1 addition & 1 deletion tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"strict": true,
"strictFunctionTypes": true,
"types": [ "node" ],
"esModuleInterop": true,
"esModuleInterop": true
},
"include": ["./src/**/*"],
"exclude": ["./src/redoc.esm.ts"]
Expand Down
20 changes: 20 additions & 0 deletions types/Addenda.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path="./File.d.ts" />

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<string>;
}
}
}
68 changes: 68 additions & 0 deletions types/Assets.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/// <reference types="node" />
/// <reference path="./Service.d.ts" />

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<object>,
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<Assets.Image>;

delete(path: string): Promise<void>;

list(path: string, options: Network.Request.Pagination.Options): Promise<Network.Request.Pagination.Response<Assets.Asset>>;

put(path: string, source: string | Buffer): Promise<Assets.Asset>;
}

}
}
35 changes: 35 additions & 0 deletions types/Cfdi.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// <reference path="./File.d.ts" />
/// <reference path="./Pdf.d.ts" />
/// <reference path="./Addenda.d.ts" />
/// <reference path="./Service.d.ts" />

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<Pdf>;
}
}
}
36 changes: 36 additions & 0 deletions types/File.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// <reference types="node" />

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;
}>;
}

}
}
37 changes: 37 additions & 0 deletions types/Global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference path="./File.d.ts" />

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<T> {
data: Array<T>;
pagination: {
limit: number;
has_more: boolean;
next_token: string | null
}

}

}
}
}
}
}
44 changes: 44 additions & 0 deletions types/Pdf.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/// <reference path="./File.d.ts" />
/// <reference path="./Cfdi.d.ts" />

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;
}
}
}
39 changes: 39 additions & 0 deletions types/Service.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// <reference types="node" />
/// <reference path="./Global.d.ts" />
/// <reference path="./Cfdi.d.ts" />
/// <reference path="./Assets.d.ts" />

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<Pdf.ConstructorParams>;

fetchAssets({ path, options }: Assets.Service.FetchParams): Promise<Network.Request.Pagination.Response<Assets.Asset>>;

putAsset({ path, file }: Assets.Service.PutParams): Promise<Assets.Asset>;

deleteAsset({ path }: Assets.Service.BasicParams): Promise<void>;

_request(endpoint: string, method: string, config?: Network.Request.Options): Promise<{
code: number;
data: any;
headers: Headers;
}>;
}

}
}

31 changes: 31 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
///<reference path='./Addenda.d.ts' />
///<reference path='./Assets.d.ts' />
///<reference path='./Cfdi.d.ts' />
///<reference path='./File.d.ts' />
///<reference path='./Pdf.d.ts' />
///<reference path='./Service.d.ts' />

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;
}

0 comments on commit 051559c

Please sign in to comment.