Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ jobs:
python-version: ['3.10', '3.11', '3.12', '3.13']
steps:
- uses: actions/checkout@v5
- name: Set up Node
uses: actions/setup-node@v5
with:
node-version: 20
cache: npm
- run: npm ci
- name: Set up Python
uses: actions/setup-python@v6
with:
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"build:css": "tailwindcss -i ./src/tailwind.css -o ./static/styles.css --minify",
"watch:css": "tailwindcss -i ./src/tailwind.css -o ./static/styles.css --watch",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"postinstall": "node scripts/ensure-castle-umd.js"
},
"repository": {
"type": "git",
Expand All @@ -19,7 +20,7 @@
},
"homepage": "https://github.com/castle/castle-python-example#readme",
"dependencies": {
"@castleio/castle-js": "^2.8.4"
"@castleio/castle-js": "^2.8.5"
},
"devDependencies": {
"tailwindcss": "^3.4.19"
Expand Down
15 changes: 15 additions & 0 deletions scripts/ensure-castle-umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fs = require('fs');
const path = require('path');

const dist = path.join(__dirname, '..', 'node_modules', '@castleio', 'castle-js', 'dist');
const dest = path.join(dist, 'castle.umd.js');
if (!fs.existsSync(dist) || fs.existsSync(dest)) {
process.exit(0);
}

const source = fs.readdirSync(dist).find((name) => (
name.startsWith('castle.') && name.endsWith('.js') && name !== 'castle.js'
));
if (source) {
fs.copyFileSync(path.join(dist, source), dest);
}
9 changes: 7 additions & 2 deletions static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ async function postJSON(url, data) {

// Resolve a Castle request token, falling back gracefully if the browser SDK
// is unavailable (e.g. no publishable key configured).
function castleClient() {
return window.__castle || window.Castle;
}

function withRequestToken(callback) {
if (window.Castle && typeof Castle.createRequestToken === "function") {
Castle.createRequestToken()
var sdk = castleClient();
if (sdk && typeof sdk.createRequestToken === "function") {
sdk.createRequestToken()
.then(callback)
.catch(function (err) {
console.error("Castle.createRequestToken failed", err);
Expand Down
12 changes: 10 additions & 2 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link href="/static/styles.css" rel="stylesheet">

<script src="/vendor/castle-js/castle.browser.js"></script>
<script>
if (!window.Castle) {
window.exports = window.exports || {};
window.module = window.module || { exports: window.exports };
window.Castle = window.module.exports;
}
</script>
<script src="/vendor/castle-js/castle.umd.js"></script>
<script>
window.Castle = window.Castle || (window.module && window.module.exports) || window["@castleio/castle-js"];
if (window.Castle && "{{ castle_pk or '' }}") {
Castle.configure({ pk: "{{ castle_pk or '' }}" });
window.__castle = Castle.configure({ pk: "{{ castle_pk or '' }}" }) || window.Castle;
}
</script>
<script src="/static/app.js" defer></script>
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ def test_unknown_demo_renders_error_page(client):
def test_unknown_vendor_asset_returns_404(client):
resp = client.get("/vendor/castle-js/nope.js")
assert resp.status_code == 404


def test_home_loads_castle_umd(client):
resp = client.get("/")
assert resp.status_code == 200
assert b"/vendor/castle-js/castle.umd.js" in resp.data


def test_castle_umd_is_served_from_npm(client):
resp = client.get("/vendor/castle-js/castle.umd.js")
assert resp.status_code == 200
assert "javascript" in resp.mimetype