Skip to content

Component Web Service Context

Created jdiamante 2024-03-14 Updated jgalland 2024-03-28

(requires core 4.2.36+)

The context object in web services allows access to backend services within a component's web service functions. This is provided in the third parameter of a component web service function next to req. This document focuses on the usage of formbirdServiceInjector for accessing the FileService, a key service for file management operations.

Accessing Services

Within a web service, the context object's formbirdServiceInjector provides access to backend services. Currently, it supports the FileService, offering functionalities for file handling.

FileService Functions

The FileService through the formbirdServiceInjector includes two main functions:

  • getFile: Matches the functionality provided in the ruleset's context, allowing file retrieval.
  • uploadFile: Similar to its counterpart in the ruleset's context, enabling file uploads.

Example Usage

{
    componentName: "ws-test-service",    
    getGreeting: function(args, req, context) {
        var deferred = q.defer();

        // Access FileService
        const FileService = context.formbirdServiceInjector.FileService;

        // Use FileService for operations
        FileService.uploadFile(/* parameters */).then(function(uploadResult) {
            deferred.resolve(uploadResult);
        }).catch(function(error) {
            deferred.reject(error);
        });

        return deferred.promise;
    }
}

This example demonstrates accessing and using the FileService for file upload within a web service.