forked from reactioncommerce/api-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support GraphQL IDs being internal
Signed-off-by: Eric Dobbertin <[email protected]>
- Loading branch information
Showing
11 changed files
with
2,008 additions
and
680 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Babel is used only for running Jest tests in API projects. | ||
* If Jest adds support for ESM and import.meta, then Babel | ||
* may become unnecessary. | ||
*/ | ||
|
||
module.exports = require("./lib/configs/babel.config.cjs"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import envalid from "envalid"; | ||
|
||
const { bool } = envalid; | ||
|
||
export default envalid.cleanEnv(process.env, { | ||
REACTION_SHOULD_ENCODE_IDS: bool({ | ||
default: true, | ||
desc: "Setting this to false makes the `encodeOpaqueId` function pass through the ID unchanged." | ||
}) | ||
}, { | ||
dotEnvPath: null | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import decodeOpaqueId from "./decodeOpaqueId.js"; | ||
|
||
test("decodes base64", () => { | ||
const encodedId = "cmVhY3Rpb24vc2hvcDpieTV3cGRnM25NcThnWDU0Yw=="; | ||
expect(decodeOpaqueId(encodedId)).toEqual({ | ||
id: "by5wpdg3nMq8gX54c", | ||
namespace: "reaction/shop" | ||
}); | ||
}); | ||
|
||
test("passes through non-base64", () => { | ||
const id = "by5wpdg3nMq8gX54c"; | ||
expect(decodeOpaqueId(id)).toEqual({ | ||
id, | ||
namespace: null | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import decodeOpaqueIdForNamespace from "./decodeOpaqueIdForNamespace.js"; | ||
|
||
test("decodes base64", () => { | ||
const encodedId = "cmVhY3Rpb24vc2hvcDpieTV3cGRnM25NcThnWDU0Yw=="; | ||
expect(decodeOpaqueIdForNamespace("reaction/shop", encodedId)).toBe("by5wpdg3nMq8gX54c"); | ||
}); | ||
|
||
test("passes through non-base64", () => { | ||
const id = "by5wpdg3nMq8gX54c"; | ||
expect(decodeOpaqueIdForNamespace("reaction/shop", id)).toBe(id); | ||
}); | ||
|
||
test("throws if base64 and namespace is wrong", () => { | ||
const encodedId = "cmVhY3Rpb24vcHJvZHVjdDpieTV3cGRnM25NcThnWDU0Yw=="; | ||
expect(() => decodeOpaqueIdForNamespace("reaction/shop", encodedId)).toThrow("ID namespace must be reaction/shop"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import config from "./config.js"; | ||
import encodeOpaqueId from "./encodeOpaqueId.js"; | ||
|
||
jest.mock("./config.js", () => ({ | ||
__esModule: true, // this property makes it work | ||
default: { | ||
REACTION_SHOULD_ENCODE_IDS: true | ||
} | ||
})); | ||
|
||
const id = "by5wpdg3nMq8gX54c"; | ||
const encodedId = "cmVhY3Rpb24vc2hvcDpieTV3cGRnM25NcThnWDU0Yw=="; | ||
|
||
test("encodes to base64", () => { | ||
expect(encodeOpaqueId("reaction/shop", id)).toBe(encodedId); | ||
}); | ||
|
||
test("skips encoding in REACTION_SHOULD_ENCODE_IDS env is false", async () => { | ||
config.REACTION_SHOULD_ENCODE_IDS = false; | ||
expect(encodeOpaqueId("reaction/shop", id)).toBe(id); | ||
config.REACTION_SHOULD_ENCODE_IDS = true; | ||
}); |
Oops, something went wrong.