11import axios , { AxiosError } from 'axios'
2- import { NightfallResponse , NightfallError , ScanText } from './types'
3-
4- export class Nightfall {
5- private readonly API_HOST = 'https://api.nightfall.ai'
6- private readonly API_KEY : string = ''
7- private readonly AXIOS_HEADERS : { [ key : string ] : string } = { }
2+ import { Base } from './base'
3+ import { FileScanner } from './filesScanner'
4+ import { NightfallResponse , NightfallError , ScanText , ScanFile } from './types'
85
6+ export class Nightfall extends Base {
97 /**
10- * Create an instance of the Nightfall client.
8+ * Create an instance of the Nightfall client. Although you can supply
9+ * your API key manually when you initiate the client, we recommend that
10+ * you configure your API key as an environment variable named
11+ * NIGHTFALL_API_KEY. The client automatically reads `process.env.NIGHTFALL_API_KEY`
12+ * when you initiate the client like so: `const client = new Nightfall()`.
1113 *
1214 * @param apiKey Your Nightfall API key
1315 */
14- constructor ( apiKey : string ) {
15- this . API_KEY = apiKey
16-
17- // Set Axios request headers since we will reuse this quite a lot
18- this . AXIOS_HEADERS = {
19- 'Authorization' : `Bearer ${ this . API_KEY } ` ,
20- 'Content-Type' : 'application/json' ,
21- "User-Agent" : "nightfall-nodejs-sdk/1.0.0"
22- }
16+ constructor ( apiKey ?: string ) {
17+ super ( apiKey )
2318 }
2419
2520 /**
@@ -30,7 +25,7 @@ export class Nightfall {
3025 *
3126 * @param payload An array of strings that you would like to scan
3227 * @param config The configuration to use to scan the payload
33- * @returns A promise object representing the Nightfall response
28+ * @returns A promise that contains the API response
3429 */
3530 async scanText ( payload : string [ ] , config : ScanText . RequestConfig ) : Promise < NightfallResponse < ScanText . Response > > {
3631 try {
@@ -54,17 +49,38 @@ export class Nightfall {
5449 }
5550
5651 /**
57- * A helpder function to determine whether the error is a generic JavaScript error or a
58- * Nightfall API error.
52+ * A utility method that wraps the four steps related to uploading and scanning files.
53+ * As the underlying file might be arbitrarily large, this scan is conducted
54+ * asynchronously. Results from the scan are delivered to the webhook URL provided in
55+ * the request.
56+ *
57+ * @see https://docs.nightfall.ai/docs/scanning-files
5958 *
60- * @param error The error object
61- * @returns A boolean that indicates if the error is a Nightfall error
59+ * @param filePath The path of the file that you wish to scan
60+ * @param policy An object containing the scan policy
61+ * @param requestMetadata Optional - A string containing arbitrary metadata. You may opt to use
62+ * this to help identify your input file upon receiving a webhook response.
63+ * Maximum length 10 KB.
64+ * @returns A promise that contains the API response
6265 */
63- private isNightfallError ( error : any ) : boolean {
64- if ( error . hasOwnProperty ( 'isAxiosError' ) && error . isAxiosError && error . response . data . hasOwnProperty ( 'code' ) ) {
65- return true
66- }
66+ async scanFile ( filePath : string , policy : ScanFile . ScanPolicy , requestMetadata ?: string ) : Promise < NightfallResponse < ScanFile . ScanResponse > > {
67+ try {
68+ const fileScanner = new FileScanner ( this . API_KEY , filePath )
69+ await fileScanner . initialize ( )
70+ await fileScanner . uploadChunks ( )
71+ await fileScanner . finish ( )
72+ const response = await fileScanner . scan ( policy , requestMetadata )
6773
68- return false
74+ return Promise . resolve ( new NightfallResponse < ScanFile . ScanResponse > ( response . data ) )
75+ } catch ( error ) {
76+ if ( this . isNightfallError ( error ) ) {
77+ const axiosError = error as AxiosError < NightfallError >
78+ const errorResponse = new NightfallResponse < ScanFile . ScanResponse > ( )
79+ errorResponse . setError ( axiosError . response ?. data as NightfallError )
80+ return Promise . resolve ( errorResponse )
81+ }
82+
83+ return Promise . reject ( error )
84+ }
6985 }
7086}
0 commit comments