Issue
I'm trying to store a session on disk for local development, and I'm running into a problem where the application expects me to return an instance of a Session and not an Object
function storeCallback(session) {
console.log("storeCallback ", session);
fs.writeFileSync("./session.json", JSON.stringify(session));
return true;
}
function loadCallback(id) {
console.log("loadCallback ", id);
const session = fs.readFileSync("./session.json", "utf8");
return JSON.parse(session);
}
The session look like this:
Session {
id: '0984e7db-ad80-4276-966d-9db54fac11c2',
shop: 'swiftnextstep.myshopify.com',
state: '113617456401679',
isOnline: true
}
Once I save and read back, it looks like this
{
id: '0984e7db-ad80-4276-966d-9db54fac11c2',
shop: 'swiftnextstep.myshopify.com',
state: '113617456401679',
isOnline: true
}
And I get an error InternalServerError: Expected return to be instance of Session, but received instance of Object.
How can I cast/transform the object I'm parsing from storage into the expected Session type?
Solution
Session looks to be exported only from session.ts
. The constructor is empty, so that's not something you have to worry about, it's just an object with properties. To turn a non-Session into a Session, you should be able to just assign to properties of a Session.
import { Session } from "@shopify/shopify-api/dist/auth/session/session";
function loadCallback(id) {
console.log("loadCallback ", id);
return Object.assign(
new Session(),
JSON.parse(fs.readFileSync("./session.json", "utf8")))
);
}
Answered By - CertainPerformance Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.