@edirect/sftp-client 11.0.62-beta.0 → 11.0.62-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # @edirect/sftp-client
2
+
3
+ SFTP client module for eDirect NestJS applications. Provides a `SftpClientService` with methods for uploading, downloading, listing, renaming, and deleting files on SFTP servers — backed by `ssh2-sftp-client`.
4
+
5
+ ## Features
6
+
7
+ - Upload files (string content or Buffer)
8
+ - Download files to a local path or writable stream
9
+ - List directory contents with optional sort by modification date
10
+ - Read file content as string
11
+ - Rename files (`rename` and `posixRename`)
12
+ - Delete files
13
+ - Connection options support both password and private key authentication
14
+ - NestJS module — injectable via `SftpClientModule`
15
+
16
+ ## Installation
17
+
18
+ ```sh
19
+ pnpm add @edirect/sftp-client
20
+ # or
21
+ npm install @edirect/sftp-client
22
+ ```
23
+
24
+ ## Setup
25
+
26
+ ### Register `SftpClientModule` in your AppModule
27
+
28
+ ```ts
29
+ import { Module } from '@nestjs/common';
30
+ import { SftpClientModule } from '@edirect/sftp-client';
31
+
32
+ @Module({
33
+ imports: [SftpClientModule],
34
+ })
35
+ export class AppModule {}
36
+ ```
37
+
38
+ ### Inject and use `SftpClientService`
39
+
40
+ ```ts
41
+ import { Injectable } from '@nestjs/common';
42
+ import { SftpClientService } from '@edirect/sftp-client';
43
+
44
+ const SFTP_OPTIONS = {
45
+ host: process.env.SFTP_HOST,
46
+ username: process.env.SFTP_USERNAME,
47
+ password: process.env.SFTP_PASSWORD,
48
+ port: process.env.SFTP_PORT ?? '22',
49
+ };
50
+
51
+ @Injectable()
52
+ export class ReportService {
53
+ constructor(private readonly sftp: SftpClientService) {}
54
+
55
+ async uploadReport(content: string, filename: string): Promise<void> {
56
+ await this.sftp.upload(SFTP_OPTIONS, content, `/reports/${filename}`);
57
+ }
58
+
59
+ async downloadReport(filename: string, localPath: string): Promise<void> {
60
+ await this.sftp.download(SFTP_OPTIONS, `/reports/${filename}`, localPath);
61
+ }
62
+
63
+ async listReports(): Promise<string[]> {
64
+ return this.sftp.listFiles(SFTP_OPTIONS, '/reports', 'DESC');
65
+ }
66
+
67
+ async readLatestReport(filename: string): Promise<string> {
68
+ return this.sftp.getFileContent(SFTP_OPTIONS, `/reports/${filename}`);
69
+ }
70
+ }
71
+ ```
72
+
73
+ ## Connection Options (`ISFTPClientOptions`)
74
+
75
+ | Option | Type | Required | Description |
76
+ | ------------ | ------------------ | -------- | ---------------------------------------- |
77
+ | `host` | `string` | Yes | SFTP server hostname or IP |
78
+ | `port` | `string` | Yes | SFTP server port (usually `'22'`) |
79
+ | `username` | `string` | Yes | SSH username |
80
+ | `password` | `string` | No | Password authentication |
81
+ | `privateKey` | `string \| Buffer` | No | Private key for key-based authentication |
82
+ | `passphrase` | `string` | No | Passphrase for the private key |
83
+ | `encoding` | `string` | No | File encoding |
84
+
85
+ Either `password` or `privateKey` must be provided.
86
+
87
+ ## API
88
+
89
+ ### `SftpClientService`
90
+
91
+ | Method | Signature | Description |
92
+ | ---------------- | ------------------------------------------------------------------------------------ | --------------------------------------------------------------------- |
93
+ | `upload` | `(options, dataToUpload: string \| Buffer, fileName: string): Promise<void>` | Upload content to `fileName` on the SFTP server |
94
+ | `download` | `(options, fileName: string, downloadPath: string \| WritableStream): Promise<void>` | Download `fileName` to a local path or writable stream |
95
+ | `delete` | `(options, deletePath: string): Promise<void>` | Delete a file at the given path |
96
+ | `rename` | `(options, fromPath: string, toPath: string): Promise<void>` | Rename a file |
97
+ | `posixRename` | `(options, fromPath: string, toPath: string): Promise<void>` | Rename using POSIX atomic rename (server-side) |
98
+ | `listFiles` | `(options, path: string, orderByModifiedDate?: 'ASC' \| 'DESC'): Promise<string[]>` | List filenames in a directory, optionally sorted by modification time |
99
+ | `getFileContent` | `(options, filePath: string): Promise<string>` | Read and return file content as a UTF-8 string |
100
+
101
+ > Each method opens a new SFTP connection, performs the operation, and closes the connection. This avoids stale connection issues in long-running services.
102
+
103
+ ## Environment Variables
104
+
105
+ The module does not read environment variables directly — connection options are passed per-call. A recommended pattern is to load them from `@edirect/config`:
106
+
107
+ ```ts
108
+ import { ConfigService } from '@edirect/config';
109
+
110
+ const options = {
111
+ host: configService.get('SFTP_HOST'),
112
+ port: configService.get('SFTP_PORT') ?? '22',
113
+ username: configService.get('SFTP_USERNAME'),
114
+ password: configService.get('SFTP_PASSWORD'),
115
+ privateKey: configService.get('SFTP_PRIVATE_KEY'),
116
+ };
117
+ ```
118
+
119
+ ## Key-Based Authentication
120
+
121
+ ```ts
122
+ import fs from 'fs';
123
+
124
+ await sftp.upload(
125
+ {
126
+ host: 'sftp.example.com',
127
+ port: '22',
128
+ username: 'deploy',
129
+ privateKey: fs.readFileSync('/path/to/id_rsa'),
130
+ passphrase: process.env.SSH_PASSPHRASE,
131
+ },
132
+ fileContent,
133
+ '/uploads/report.csv',
134
+ );
135
+ ```
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@edirect/sftp-client",
3
+ "version": "11.0.62-beta.2",
4
+ "main": "./dist/src/index.js",
5
+ "types": "./dist/src/index.d.ts",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/src/index.js",
9
+ "default": "./dist/src/index.js",
10
+ "require": "./dist/src/index.js",
11
+ "types": "./dist/src/index.d.ts"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "dependencies": {
19
+ "@nestjs/common": "^11.1.19",
20
+ "ssh2-sftp-client": "^12.1.1",
21
+ "tslib": "^2.8.1"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "^25.6.2",
25
+ "@types/ssh2-sftp-client": "^9.0.6"
26
+ },
27
+ "type": "commonjs"
28
+ }
@@ -0,0 +1,3 @@
1
+ export { SftpClientModule } from './sftp-client.module';
2
+ export { SftpClientService } from './sftp-client.service';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC"}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SftpClientService = exports.SftpClientModule = void 0;
4
+ var sftp_client_module_1 = require("./sftp-client.module");
5
+ Object.defineProperty(exports, "SftpClientModule", { enumerable: true, get: function () { return sftp_client_module_1.SftpClientModule; } });
6
+ var sftp_client_service_1 = require("./sftp-client.service");
7
+ Object.defineProperty(exports, "SftpClientService", { enumerable: true, get: function () { return sftp_client_service_1.SftpClientService; } });
@@ -0,0 +1,19 @@
1
+ export interface ISFTPClientOptions {
2
+ host: string;
3
+ username: string;
4
+ password?: string;
5
+ passphrase?: string;
6
+ port: string;
7
+ encoding?: string;
8
+ privateKey?: string | Buffer;
9
+ }
10
+ export interface ISftpClientInterface {
11
+ upload(options: ISFTPClientOptions, dataToUpload: string | Buffer, fileName: string): Promise<void>;
12
+ download(options: ISFTPClientOptions, fileName: string, downloadPath: string | NodeJS.WritableStream): Promise<void>;
13
+ delete(options: ISFTPClientOptions, deletePath: string): Promise<void>;
14
+ posixRename(options: ISFTPClientOptions, fromPath: string, toPath: string): Promise<void>;
15
+ rename(options: ISFTPClientOptions, fromPath: string, toPath: string): Promise<void>;
16
+ listFiles(options: ISFTPClientOptions, path: string, orderByModifiedDate?: 'ASC' | 'DESC'): Promise<string[]>;
17
+ getFileContent(options: ISFTPClientOptions, filePath: string): Promise<string>;
18
+ }
19
+ //# sourceMappingURL=sftp-client.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sftp-client.interface.d.ts","sourceRoot":"","sources":["../../src/sftp-client.interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MA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3
+ //# sourceMappingURL=sftp-client.module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sftp-client.module.d.ts","sourceRoot":"","sources":["../../src/sftp-client.module.ts"],"names":[],"mappings":"AAGA,qBAIa,gBAAgB;CAAG"}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SftpClientModule = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@nestjs/common");
6
+ const sftp_client_service_1 = require("./sftp-client.service");
7
+ let SftpClientModule = class SftpClientModule {
8
+ };
9
+ exports.SftpClientModule = SftpClientModule;
10
+ exports.SftpClientModule = SftpClientModule = tslib_1.__decorate([
11
+ (0, common_1.Module)({
12
+ providers: [sftp_client_service_1.SftpClientService],
13
+ exports: [sftp_client_service_1.SftpClientService],
14
+ })
15
+ ], SftpClientModule);
@@ -0,0 +1,13 @@
1
+ import { ISftpClientInterface, ISFTPClientOptions } from './sftp-client.interface';
2
+ export declare class SftpClientService implements ISftpClientInterface {
3
+ upload(options: ISFTPClientOptions, dataToUpload: string | Buffer, fileName: string): Promise<void>;
4
+ download(options: ISFTPClientOptions, fileName: string, downloadPath: string | NodeJS.WritableStream): Promise<void>;
5
+ delete(options: ISFTPClientOptions, deletePath: string): Promise<void>;
6
+ posixRename(options: ISFTPClientOptions, fromPath: string, toPath: string): Promise<void>;
7
+ rename(options: ISFTPClientOptions, fromPath: string, toPath: string): Promise<void>;
8
+ listFiles(options: ISFTPClientOptions, path: string, orderByModifiedDate?: 'ASC' | 'DESC'): Promise<string[]>;
9
+ getFileContent(options: ISFTPClientOptions, filePath: string): Promise<string>;
10
+ private connectToSftp;
11
+ private getSftpOptions;
12
+ }
13
+ //# sourceMappingURL=sftp-client.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sftp-client.service.d.ts","sourceRoot":"","sources":["../../src/sftp-client.service.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,oBAAoB,EACpB,kBAAkB,EACnB,MAAM,yBAAyB,CAAC;AAEjC,qBACa,iBAAkB,YAAW,oBAAoB;IAC/C,MAAM,CACjB,OAAO,EAAE,kBAAkB,EAC3B,YAAY,EAAE,MAAM,GAAG,MAAM,EAC7B,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAUH,QAAQ,CACnB,OAAO,EAAE,kBAAkB,EAC3B,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,MAAM,GAAG,MAAM,CAAC,cAAc,GAC3C,OAAO,CAAC,IAAI,CAAC;IAUH,MAAM,CACjB,OAAO,EAAE,kBAAkB,EAC3B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC;IAUH,WAAW,CACtB,OAAO,EAAE,kBAAkB,EAC3B,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,IAAI,CAAC;IAUH,MAAM,CACjB,OAAO,EAAE,kBAAkB,EAC3B,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,IAAI,CAAC;IAUH,SAAS,CACpB,OAAO,EAAE,kBAAkB,EAC3B,IAAI,EAAE,MAAM,EACZ,mBAAmB,CAAC,EAAE,KAAK,GAAG,MAAM,GACnC,OAAO,CAAC,MAAM,EAAE,CAAC;IAqBP,cAAc,CACzB,OAAO,EAAE,kBAAkB,EAC3B,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,MAAM,CAAC;YAcJ,aAAa;IAO3B,OAAO,CAAC,cAAc;CAkBvB"}
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SftpClientService = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@nestjs/common");
6
+ const ssh2_sftp_client_1 = tslib_1.__importDefault(require("ssh2-sftp-client"));
7
+ let SftpClientService = class SftpClientService {
8
+ async upload(options, dataToUpload, fileName) {
9
+ try {
10
+ const SFTP = await this.connectToSftp(options);
11
+ await SFTP.put(Buffer.from(dataToUpload), fileName);
12
+ await SFTP.end();
13
+ }
14
+ catch (error) {
15
+ throw new Error(`Error on save ${fileName} on SFTP Server: \n ${error}`);
16
+ }
17
+ }
18
+ async download(options, fileName, downloadPath) {
19
+ try {
20
+ const SFTP = await this.connectToSftp(options);
21
+ await SFTP.get(fileName, downloadPath);
22
+ await SFTP.end();
23
+ }
24
+ catch (error) {
25
+ throw new Error(`Error on download ${fileName}: \n ${error}`);
26
+ }
27
+ }
28
+ async delete(options, deletePath) {
29
+ try {
30
+ const SFTP = await this.connectToSftp(options);
31
+ await SFTP.delete(deletePath);
32
+ await SFTP.end();
33
+ }
34
+ catch (error) {
35
+ throw new Error(`Error on delete ${deletePath}: \n ${error}`);
36
+ }
37
+ }
38
+ async posixRename(options, fromPath, toPath) {
39
+ try {
40
+ const SFTP = await this.connectToSftp(options);
41
+ await SFTP.posixRename(fromPath, toPath);
42
+ await SFTP.end();
43
+ }
44
+ catch (error) {
45
+ throw new Error(`Error on rename ${fromPath}: \n ${error}`);
46
+ }
47
+ }
48
+ async rename(options, fromPath, toPath) {
49
+ try {
50
+ const SFTP = await this.connectToSftp(options);
51
+ await SFTP.rename(fromPath, toPath);
52
+ await SFTP.end();
53
+ }
54
+ catch (error) {
55
+ throw new Error(`Error on rename ${fromPath}: \n ${error}`);
56
+ }
57
+ }
58
+ async listFiles(options, path, orderByModifiedDate) {
59
+ try {
60
+ const SFTP = await this.connectToSftp(options);
61
+ const list = await SFTP.list(path);
62
+ await SFTP.end();
63
+ return list
64
+ .sort((currentFile, nextFile) => {
65
+ if (orderByModifiedDate === 'ASC') {
66
+ return currentFile.modifyTime - nextFile.modifyTime;
67
+ }
68
+ if (orderByModifiedDate === 'DESC') {
69
+ return nextFile.modifyTime - currentFile.modifyTime;
70
+ }
71
+ return 0;
72
+ })
73
+ .map((file) => file.name);
74
+ }
75
+ catch (error) {
76
+ throw new Error(`Error on listing files ${path}: \n ${error}`);
77
+ }
78
+ }
79
+ async getFileContent(options, filePath) {
80
+ try {
81
+ const SFTP = await this.connectToSftp(options);
82
+ const fileBuffer = await SFTP.get(filePath);
83
+ await SFTP.end();
84
+ if (Buffer.isBuffer(fileBuffer)) {
85
+ return fileBuffer.toString();
86
+ }
87
+ return fileBuffer;
88
+ }
89
+ catch (error) {
90
+ throw new Error(`Error on getting file content ${filePath}: \n ${error}`);
91
+ }
92
+ }
93
+ async connectToSftp(options) {
94
+ const sftpOptions = this.getSftpOptions(options);
95
+ const client = new ssh2_sftp_client_1.default();
96
+ await client.connect(sftpOptions);
97
+ return client;
98
+ }
99
+ getSftpOptions(options) {
100
+ const { host, password, port, username, encoding, privateKey, passphrase } = options;
101
+ if (!host || (!password && !privateKey) || !port || !username) {
102
+ throw new Error('Options parameter is required.');
103
+ }
104
+ return {
105
+ host,
106
+ username,
107
+ password,
108
+ passphrase,
109
+ port: Number(port),
110
+ encoding,
111
+ privateKey,
112
+ };
113
+ }
114
+ };
115
+ exports.SftpClientService = SftpClientService;
116
+ exports.SftpClientService = SftpClientService = tslib_1.__decorate([
117
+ (0, common_1.Injectable)()
118
+ ], SftpClientService);
@@ -0,0 +1 @@
1
+ {"version":"5.9.3"}
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@edirect/sftp-client",
3
- "version": "11.0.62-beta.0",
3
+ "version": "11.0.62-beta.2",
4
+ "packageScope": "@edirect",
4
5
  "main": "./dist/src/index.js",
5
6
  "types": "./dist/src/index.d.ts",
6
7
  "exports": {
@@ -24,5 +25,21 @@
24
25
  "@types/node": "^25.6.2",
25
26
  "@types/ssh2-sftp-client": "^9.0.6"
26
27
  },
27
- "type": "commonjs"
28
+ "nx": {
29
+ "name": "@edirect/sftp-client",
30
+ "targets": {
31
+ "build": {
32
+ "executor": "@nx/js:tsc",
33
+ "options": {
34
+ "main": "{workspaceRoot}/packages/edirect-sftp-client/src/index.ts",
35
+ "tsConfig": "{workspaceRoot}/packages/edirect-sftp-client/tsconfig.lib.json",
36
+ "outputPath": "{workspaceRoot}/packages/edirect-sftp-client/dist",
37
+ "assets": [
38
+ "{workspaceRoot}/packages/edirect-sftp-client/package.json",
39
+ "{workspaceRoot}/packages/edirect-sftp-client/README.md"
40
+ ]
41
+ }
42
+ }
43
+ }
44
+ }
28
45
  }