Function submit

  • Submits the given action by making an HTTP request according to action's method and href.

    If fields are present in action, they are validated via options.validator. A ValidationError is thrown when options.validator returns a NegativeValidationResult. If options.validator is not provided, then validation automatically passes.

    If validation passes, the fields are then serialized according to options.serializer, which receives action's type and fields. If options.serializer is not provided, the defaultSerializer is used. If action.method is 'GET' or 'DELETE', the serialized content is placed in the query string. Otherwise, the content is placed in the request body.

    Example

    import { defaultSerializer } from '@siren-js/client';

    await submit(action, {
    validator: (fields) => {
    // ensure each field has a non-nullish value
    if (fields.every((field) => field.value != null))
    return new PositiveValidationResult();
    else
    return new NegativeValidationResult();
    },

    serializer: (type, fields) => {
    if (type === 'text/xml')
    return {
    content: // serialize fields to XML however you like...
    };
    else
    // rely on default serializer for any other type
    return defaultSerializer(type, field);
    }
    })

    Returns

    a Promise that fulfills with an HTTP Response object

    Throws

    a ValidationError when options.validator returns a NegativeValidationResult

    Parameters

    Returns Promise<Response>

Generated using TypeDoc