import axios from 'axios';
const { request } = axios;
/**
 * Client for EAS interaction, specifically fetching entity object.
 */
class Eas {
    /**
     * Create an object for accessing an Entity Attribute Service.
     * @param {object} config - options to  configure this EAS accessor
     * @param {AuthProvider|function} config.authProvider - interceptor for `http-request.Request` object manipulation
     * @param {string} config.endpoint - the URI to connect to
     * @param {function} [config.requestFunctor=request] - http request async function object
     */
    constructor({ authProvider, endpoint, requestFunctor, }) {
        this.authProvider = authProvider;
        this.endpoint = endpoint;
        this.requestFunctor = requestFunctor || request;
    }
    /**
     * Request an entity object for the current user.
     * @param {object} config - options for the request
     * @param {string} config.publicKey - String encoded public key from the keypair to be used with any subsequent requests refering to the returned EO
     * @param {object} [config.etc] - additional parameters to be passed to the EAS entity-object endpoint
     */
    async fetchEntityObject({ publicKey, ...etc }) {
        // Create a skeleton http request for EAS.
        const incredibleHttpReq = {
            url: this.endpoint,
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: { publicKey, ...etc },
        };
        // Delegate modifications to the auth provider.
        // TODO: Handle various exception cases from interface docs.
        const httpReq = await this.authProvider.withCreds(incredibleHttpReq);
        // Execute the http request using axios.
        const axiosParams = {
            method: httpReq.method,
            headers: httpReq.headers,
            url: httpReq.url,
            params: undefined,
            data: undefined,
        };
        // Allow the authProvider to change the method.
        if (httpReq.method === 'POST' || httpReq.method === 'PATCH' || httpReq.method === 'PUT') {
            axiosParams.data = httpReq.body;
        }
        else {
            axiosParams.params = httpReq.body;
        }
        return (await this.requestFunctor(axiosParams)).data;
    }
}
export default Eas;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiRWFzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2F1dGgvRWFzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBc0QsTUFBTSxPQUFPLENBQUM7QUFJM0UsTUFBTSxFQUFFLE9BQU8sRUF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