2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2020-09-17 19:08:06 +00:00
|
|
|
import ApplicationSerializer from '../application';
|
|
|
|
|
|
|
|
export default ApplicationSerializer.extend({
|
2023-02-10 19:31:47 +00:00
|
|
|
primaryKey: 'name',
|
|
|
|
|
2020-09-17 19:08:06 +00:00
|
|
|
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
|
2023-02-10 19:31:47 +00:00
|
|
|
if (payload.data?.alphabet) {
|
2020-09-17 19:08:06 +00:00
|
|
|
payload.data.alphabet = [payload.data.alphabet];
|
|
|
|
}
|
2022-02-07 20:07:53 +00:00
|
|
|
// strip out P character from any named capture groups
|
2023-02-10 19:31:47 +00:00
|
|
|
if (payload.data?.pattern) {
|
2022-02-07 20:07:53 +00:00
|
|
|
this._formatNamedCaptureGroups(payload.data, '?P', '?');
|
|
|
|
}
|
2020-09-17 19:08:06 +00:00
|
|
|
return this._super(store, primaryModelClass, payload, id, requestType);
|
|
|
|
},
|
|
|
|
|
|
|
|
serialize() {
|
2022-11-09 23:15:31 +00:00
|
|
|
const json = this._super(...arguments);
|
2020-09-17 19:08:06 +00:00
|
|
|
if (json.alphabet && Array.isArray(json.alphabet)) {
|
|
|
|
// Templates should only ever have one alphabet
|
|
|
|
json.alphabet = json.alphabet[0];
|
|
|
|
}
|
2022-02-07 20:07:53 +00:00
|
|
|
// add P character to any named capture groups
|
|
|
|
if (json.pattern) {
|
|
|
|
this._formatNamedCaptureGroups(json, '?', '?P');
|
|
|
|
}
|
2020-09-17 19:08:06 +00:00
|
|
|
return json;
|
|
|
|
},
|
|
|
|
|
2022-02-07 20:07:53 +00:00
|
|
|
_formatNamedCaptureGroups(json, replace, replaceWith) {
|
|
|
|
// named capture groups are handled differently between Go and js
|
|
|
|
// first look for named capture groups in pattern string
|
|
|
|
const regex = new RegExp(/\?P?(<(.+?)>)/, 'g');
|
|
|
|
const namedGroups = json.pattern.match(regex);
|
|
|
|
if (namedGroups) {
|
|
|
|
namedGroups.forEach((group) => {
|
|
|
|
// add or remove P depending on destination
|
|
|
|
json.pattern = json.pattern.replace(group, group.replace(replace, replaceWith));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-09-17 19:08:06 +00:00
|
|
|
extractLazyPaginatedData(payload) {
|
2022-11-09 23:15:31 +00:00
|
|
|
return payload.data.keys.map((key) => {
|
|
|
|
const model = {
|
2020-09-17 19:08:06 +00:00
|
|
|
id: key,
|
2023-02-10 19:31:47 +00:00
|
|
|
name: key,
|
2020-09-17 19:08:06 +00:00
|
|
|
};
|
|
|
|
if (payload.backend) {
|
|
|
|
model.backend = payload.backend;
|
|
|
|
}
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|