From c349a106138f75e422f9f6c4b51eccfe17a7a5e0 Mon Sep 17 00:00:00 2001
From: Brad Overton <bradley.overton@3plearning.com>
Date: Fri, 24 Nov 2023 12:27:37 +1000
Subject: [PATCH 1/2] chore: ember-data 5.0 compatibility, use local copy of
 errorsHashToArray

---
 addon/active-model-adapter.ts | 39 ++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/addon/active-model-adapter.ts b/addon/active-model-adapter.ts
index 8d90a84..5ee004f 100644
--- a/addon/active-model-adapter.ts
+++ b/addon/active-model-adapter.ts
@@ -1,7 +1,6 @@
 import RESTAdapter from '@ember-data/adapter/rest';
 import AdapterError, {
   InvalidError,
-  errorsHashToArray,
 } from '@ember-data/adapter/error';
 import { pluralize } from 'ember-inflector';
 import { AnyObject } from 'active-model-adapter';
@@ -17,6 +16,44 @@ interface ActiveModelPayload {
   @module ember-data
 */
 
+const PRIMARY_ATTRIBUTE_KEY = 'base';
+function makeArray<T>(value: T | T[]): T[] {
+  return Array.isArray(value) ? value : [value];
+}
+type JsonApiError = Record<string, unknown>
+/**
+ * Copied from ember-data's 4.12.4 implementation after removal in ember-data 5.0
+ * https://github.com/emberjs/data/blob/a0d60c073bc03631277b258aaaadfc910e0a31dc/packages/adapter/src/error.js#L395-L429
+ * Types inferred from similar private function
+ * https://github.com/emberjs/data/blob/84a15401a7d2a6f8a7efcc2492834c80a27afcc0/packages/legacy-compat/src/legacy-network-handler/legacy-network-handler.ts#L265-L289
+ */
+function errorsHashToArray(errors: Record<string, string | string[]>): JsonApiError[] {
+  const out: JsonApiError[] = [];
+
+  if (errors) {
+    Object.keys(errors).forEach((key) => {
+      const messages = makeArray(errors[key]);
+      for (let i = 0; i < messages.length; i++) {
+        let title = 'Invalid Attribute';
+        let pointer = `/data/attributes/${key}`;
+        if (key === PRIMARY_ATTRIBUTE_KEY) {
+          title = 'Invalid Document';
+          pointer = `/data`;
+        }
+        out.push({
+          title: title,
+          detail: messages[i],
+          source: {
+            pointer: pointer,
+          },
+        });
+      }
+    });
+  }
+
+  return out;
+}
+
 /**
  * The ActiveModelAdapter is a subclass of the RESTAdapter designed to integrate
  * with a JSON API that uses an underscored naming convention instead of camelCasing.

From 8f63c00e87a81eb72c78ee4bc327828a5350da96 Mon Sep 17 00:00:00 2001
From: Brad Overton <bradley.overton@3plearning.com>
Date: Fri, 24 Nov 2023 12:32:48 +1000
Subject: [PATCH 2/2] chore: fix type

---
 addon/active-model-adapter.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/addon/active-model-adapter.ts b/addon/active-model-adapter.ts
index 5ee004f..c67068b 100644
--- a/addon/active-model-adapter.ts
+++ b/addon/active-model-adapter.ts
@@ -27,7 +27,7 @@ type JsonApiError = Record<string, unknown>
  * Types inferred from similar private function
  * https://github.com/emberjs/data/blob/84a15401a7d2a6f8a7efcc2492834c80a27afcc0/packages/legacy-compat/src/legacy-network-handler/legacy-network-handler.ts#L265-L289
  */
-function errorsHashToArray(errors: Record<string, string | string[]>): JsonApiError[] {
+function errorsHashToArray(errors: Record<string, unknown | unknown[]>): JsonApiError[] {
   const out: JsonApiError[] = [];
 
   if (errors) {