Client.js:
- Import the modules AuthContext from './Authenticate.js', Printer from './Printer.js', Scanner from './Scanner.js'
Define the class Client with:
-
The constructor that takes the parameters: printerEmail, clientId, clientSecret, baseUrl with a default value of 'https://api.epsonconnect.com'
- If printerEmail is not provided, get it from the environment variables, throw error if not present
- If clientId is not provided, get it from the environment variables, throw error if not present
- If clientSecret is not provided, get it from the environment variables, throw error if not present
- Initialize an instance of AuthContext (authContext) with baseUrl, printerEmail, clientId, clientSecret
-
A method 'initialize' that calls the '_initialize' method of the 'authContext' instance
-
A method 'deauthenticate' that calls the '_deauthenticate' method of the 'authContext' instance
-
A getter 'printer' that creates and returns a new Printer instance using the 'authContext' instance
-
A getter 'scanner' that creates and returns a new Scanner instance using the 'authContext' instance
Define the class ClientError which extends the base Error class:
- The constructor takes a parameter 'message' and sets its 'name' property to 'ClientError'
Export the classes Client and ClientError
Authenticate.js:
- Import the axios and moment libraries, and the utility function extractKeyValuePairs from './Utils.js'
Define the class AuthContext with:
-
The constructor that takes the parameters: baseUrl, printerEmail, clientId, clientSecret and initializes several instance variables: baseUrl, printerEmail, clientId, clientSecret, expiresAt, accessToken, refreshToken, and subjectId
-
A private method '_initialize' that calls the '_auth' method
-
A private method '_auth' that:
- Checks if the current access token expiry time is after the current moment
- Defines headers and authentication details
- Prepares a request payload depending on whether an access token already exists or not
- Sends a POST request to a given endpoint to retrieve an access token
- If there's an error, it throws an AuthenticationError
- If it's the first time authenticating, it sets the refresh token
- Updates the expiry time, access token, and subjectId
-
A private method '_deauthenticate' that sends a DELETE request to a specified endpoint to deauthenticate
-
A method 'send' that:
- Checks if 'auth' is not provided, if so calls the '_auth' method
- Defines default headers if 'headers' is not provided
- Sends an HTTP request with the provided parameters
- If there's an error in the response, it throws an ApiError
- Returns the response data
-
A getter 'defaultHeaders' that returns a default headers object with the access token
-
A getter 'deviceId' that returns the subjectId
Define the class AuthenticationError which extends the base Error class:
- The constructor takes a parameter 'message' and sets its 'name' property to 'AuthenticationError'
Define the class ApiError which extends the base Error class:
- The constructor takes a parameter 'message' and sets its 'name' property to 'ApiError'
Export the classes AuthContext, AuthenticationError, and ApiError
Printer.js:
Import required modules: URL, URLSearchParams from 'url', fs from 'fs', AuthContext from 'Authenticate.js', and mergeWithDefaultSettings, validateSettings from 'PrinterSettings'.
Define the Printer class with:
-
A constructor that accepts an authContext argument. It throws a PrinterError if the authContext is not an instance of AuthContext. It also initializes the VALID_EXTENSIONS and VALID_OPERATORS sets.
-
A getter method 'deviceId' that retrieves the deviceId from the authContext instance.
-
The method 'capabilities(mode)' which fetches the capabilities of the printer for a specified mode by sending a GET request to a specific endpoint.
-
The method 'printSetting(settings)' that validates and merges provided settings with default settings, sends them to a specific endpoint via a POST request, and adds the settings object to the returned response.
-
The method 'uploadFile(uploadUri, filePath, printMode)' that validates the extension of the file, reads the file content, and sends it to the provided upload URI via a POST request.
-
The method 'executePrint(jobId)' that sends a POST request to execute a print job.
-
The method 'print(filePath, settings = {})' that initiates a print process by calling 'printSetting', 'uploadFile', and 'executePrint' methods sequentially, and finally returns the jobId.
-
The method 'cancelPrint(jobId, operatedBy = 'user')' that validates the operator, checks the job status and if it's cancelable, then sends a POST request to cancel the job.
-
The method 'jobInfo(jobId)' that retrieves information about a specific job by sending a GET request.
-
The method 'info()' that retrieves information about the printer by sending a GET request.
-
The method 'notification(callbackUri, enabled = true)' that sets up notifications by sending a POST request with notification settings.
Define the PrinterError class, which extends the built-in Error class.
Finally, export the Printer and PrinterError classes.
PrinterSettings.js:
-
Define several Set constants for different printer settings:
- VALID_PRINT_MODES: contains 'document', 'photo'
- VALID_MEDIA_SIZES: contains 'ms_a3', 'ms_a4', 'ms_a5', etc.
- VALID_MEDIA_TYPES: contains 'mt_plainpaper', 'mt_photopaper', 'mt_hagaki', etc.
- VALID_PRINT_QUALITIES: contains 'high', 'normal', 'draft'
- VALID_PAPER_SOURCES: contains 'auto', 'rear', 'front1', etc.
- VALID_COLOR_MODES: contains 'color', 'mono'
- VALID_TWO_SIDE: contains 'none', 'long', 'short'
-
Define a function called
generateRandomString(length)
:- The function generates a random string of a specified length.
-
Define a function
mergeWithDefaultSettings(settings = {})
:- The function combines user-supplied print settings with default values.
- If the user does not supply a certain setting, the function uses a default value.
- The function returns the combined settings object.
-
Define a function
validateSettings(settings = {})
:- The function validates the user-supplied settings based on the valid constants defined at the start of the module.
- If a setting is invalid, the function throws a
PrintSettingError
. - It checks for valid keys, length of job name, print mode, media size, media type, boolean checks for borderless and reverse order, and the rest of the print settings.
- It has specific checks for print mode with reverse order, and two-sided printing with collation.
-
Define a class
PrintSettingError
which extends the built-in JavaScriptError
class:- The constructor of this class accepts a
message
parameter. - It sets its own
name
property to 'PrintSettingError'.
- The constructor of this class accepts a
-
The module exports the following:
mergeWithDefaultSettings
function,validateSettings
function, and thePrintSettingError
class.
Scanner.js:
-
Import
AuthContext
from './Authenticate' -
Define a class
Scanner
with the following methods and properties:-
constructor(authContext)
: Takes in anAuthContext
object as a parameter. Throws aScannerError
if the provided argument is not an instance ofAuthContext
. Initializes a_authContext
,_path
,_destination_cache
, andVALID_DESTINATION_TYPES
properties on the instance. -
VALID_DESTINATION_TYPES
: A Set that contains 'mail' and 'url' as valid types of scan destinations. -
list()
: An asynchronous method that uses the_authContext
to send a 'GET' request to the_path
. -
add(name, destination, type_ = 'mail')
: An asynchronous method that adds a new scan destination. It takesname
,destination
, andtype_
as parameters, validates these using the_validateDestination
method, prepares a data object, and sends a 'POST' request to_path
using_authContext
. It then adds the response to the_destination_cache
. -
update(id_, name = null, destination = null, type_ = null)
: An asynchronous method that updates a scan destination. It retrieves the scan destination from the_destination_cache
based on theid_
, validates thename
,destination
, andtype_
, prepares a data object, and sends a 'POST' request to update the scan destination. It then replaces the updated scan destination in the_destination_cache
. -
remove(id_)
: An asynchronous method that deletes a scan destination. It sends a 'DELETE' request to the_path
and removes the scan destination from the_destination_cache
. -
_validateDestination(name, destination, type_)
: A private method that validates thename
,destination
, andtype_
parameters according to specific rules. If any of the validations fails, it throws aScannerError
.
-
-
Define a class
ScannerError
that extends the baseError
class. -
Export the
Scanner
andScannerError
classes.
Utils.js:
-
Define a function
extractKeyValuePairs(obj, keysToExtract)
, which extracts key-value pairs from a nested object where the key is inkeysToExtract
. It takes two arguments,obj
andkeysToExtract
.-
Create an empty array
result
to store the final key-value pairs, and aseenObjects
WeakSet to avoid circular references. -
Define a nested function
extractPairsRecursive(obj, path = '')
that takesobj
andpath
as arguments. This function iterates through each key-value pair of the object:-
If
obj
is already inseenObjects
, the function returns early to avoid infinite loops due to circular references. Otherwise, it addsobj
toseenObjects
. -
It constructs the
currentPath
by concatenating thepath
and thekey
. Ifpath
is empty,currentPath
equalskey
. -
If
currentPath
is inkeysToExtract
, it pushes an object withkey
andvalue
intoresult
. -
If the
value
is a non-null object, it recursively callsextractPairsRecursive
onvalue
withcurrentPath
.
-
-
Call
extractPairsRecursive
on the inputobj
. -
After finishing the recursive traversal, return
result
.
-
-
Export the
extractKeyValuePairs
function.