github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/adapters/variable.js (about)

     1  import ApplicationAdapter from './application';
     2  import { pluralize } from 'ember-inflector';
     3  import classic from 'ember-classic-decorator';
     4  import { ConflictError } from '@ember-data/adapter/error';
     5  
     6  @classic
     7  export default class VariableAdapter extends ApplicationAdapter {
     8    pathForType = () => 'var';
     9  
    10    // PUT instead of POST on create;
    11    // /v1/var instead of /v1/vars on create (urlForFindRecord)
    12    createRecord(_store, type, snapshot) {
    13      let data = this.serialize(snapshot);
    14      let baseUrl = this.buildURL(type.modelName, data.ID);
    15      const checkAndSetValue = snapshot?.attr('modifyIndex') || 0;
    16      return this.ajax(`${baseUrl}?cas=${checkAndSetValue}`, 'PUT', { data });
    17    }
    18  
    19    urlForFindAll(modelName) {
    20      let baseUrl = this.buildURL(modelName);
    21      return pluralize(baseUrl);
    22    }
    23  
    24    urlForQuery(_query, modelName) {
    25      let baseUrl = this.buildURL(modelName);
    26      return pluralize(baseUrl);
    27    }
    28  
    29    urlForFindRecord(identifier, modelName, snapshot) {
    30      const { namespace, id } = _extractIDAndNamespace(identifier, snapshot);
    31      let baseUrl = this.buildURL(modelName, id);
    32      return `${baseUrl}?namespace=${namespace}`;
    33    }
    34  
    35    urlForUpdateRecord(identifier, modelName, snapshot) {
    36      const { id } = _extractIDAndNamespace(identifier, snapshot);
    37      let baseUrl = this.buildURL(modelName, id);
    38      if (snapshot?.adapterOptions?.overwrite) {
    39        return `${baseUrl}`;
    40      } else {
    41        const checkAndSetValue = snapshot?.attr('modifyIndex') || 0;
    42        return `${baseUrl}?cas=${checkAndSetValue}`;
    43      }
    44    }
    45  
    46    urlForDeleteRecord(identifier, modelName, snapshot) {
    47      const { namespace, id } = _extractIDAndNamespace(identifier, snapshot);
    48      const baseUrl = this.buildURL(modelName, id);
    49      return `${baseUrl}?namespace=${namespace}`;
    50    }
    51  
    52    handleResponse(status, _, payload) {
    53      if (status === 409) {
    54        return new ConflictError([
    55          { detail: _normalizeConflictErrorObject(payload), status: 409 },
    56        ]);
    57      }
    58      return super.handleResponse(...arguments);
    59    }
    60  }
    61  
    62  function _extractIDAndNamespace(identifier, snapshot) {
    63    const namespace = snapshot?.attr('namespace') || 'default';
    64    const id = snapshot?.attr('path') || identifier;
    65    return {
    66      namespace,
    67      id,
    68    };
    69  }
    70  
    71  function _normalizeConflictErrorObject(conflictingVariable) {
    72    return {
    73      modifyTime: Math.floor(conflictingVariable.ModifyTime / 1000000),
    74      items: conflictingVariable.Items,
    75    };
    76  }