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
7 changes: 7 additions & 0 deletions .changeset/hip-donkeys-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@spur.us/monocle-backend': minor
'@spur.us/monocle-nextjs': minor
'@spur.us/monocle-react': minor
---

Allow overriding of Monocle domain
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"release:canary": "changeset publish --tag canary",
"test": "turbo run test",
"version-packages": "changeset version && pnpm install --lockfile-only --engine-strict=false",
"version-packages:canary": "./scripts/canary.mjs",
"version-packages:canary": "changeset version --snapshot canary",
"yalc:all": "for d in packages/*/; do echo $d; cd $d; pnpm yalc push --replace --sig; cd '../../'; done"
},
"devDependencies": {
Expand All @@ -41,8 +41,7 @@
"typescript-eslint": "^8.31.0",
"vitest": "^3.1.2",
"yalc": "1.0.0-pre.53",
"yaml": "^2.7.1",
"zx": "catalog:repo"
"yaml": "^2.7.1"
},
"packageManager": "pnpm@10.8.0",
"engines": {
Expand Down
8 changes: 4 additions & 4 deletions packages/monocle-backend/src/__tests__/monocle-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ vi.mock('jose', () => ({

describe('MonocleClient', () => {
const mockSecretKey = 'test-secret-key';
const mockBaseUrl = 'https://api.test.com';
const mockBaseDomain = 'test.com';
const mockEncryptedAssessment = 'encrypted-data';
const mockDecryptedAssessment: MonocleAssessment = {
vpn: false,
Expand All @@ -38,7 +38,7 @@ describe('MonocleClient', () => {
beforeEach(() => {
client = createMonocleClient({
secretKey: mockSecretKey,
baseUrl: mockBaseUrl,
baseDomain: mockBaseDomain,
});
vi.clearAllMocks();
});
Expand All @@ -47,7 +47,7 @@ describe('MonocleClient', () => {
it('should throw error when secret key is missing', () => {
expect(() =>
createMonocleClient({
baseUrl: mockBaseUrl,
baseUrl: mockBaseDomain,
// @ts-expect-error Testing missing secretKey
secretKey: undefined,
})
Expand All @@ -66,7 +66,7 @@ describe('MonocleClient', () => {
const result = await client.decryptAssessment(mockEncryptedAssessment);

expect(global.fetch).toHaveBeenCalledWith(
`${mockBaseUrl}/api/v1/assessment`,
`https://decrypt.${mockBaseDomain}/api/v1/assessment`,
{
method: 'POST',
headers: {
Expand Down
2 changes: 1 addition & 1 deletion packages/monocle-backend/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const API_URL = 'https://decrypt.mcl.spur.us';
export const BASE_DOMAIN = 'mcl.spur.us';
export const USER_AGENT = `${PACKAGE_NAME}@${PACKAGE_VERSION}`;
8 changes: 4 additions & 4 deletions packages/monocle-backend/src/monocleClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MonocleAssessment } from '@spur.us/types';
import * as jose from 'jose';
import { API_URL, USER_AGENT } from './constants.js';
import { BASE_DOMAIN, USER_AGENT } from './constants.js';
import { MonocleOptions } from './types.js';
import {
MonocleAPIError,
Expand All @@ -24,7 +24,7 @@ export class MonocleClient {
/** The secret key used for API authentication */
private secretKey: string;
/** The base URL for API requests */
private baseUrl: string;
private decryptApiUrl: string;

/**
* Creates a new MonocleClient instance
Expand All @@ -37,7 +37,7 @@ export class MonocleClient {
});
}
this.secretKey = options.secretKey;
this.baseUrl = options.baseUrl || API_URL;
this.decryptApiUrl = `https://decrypt.${options.baseDomain || BASE_DOMAIN}`;
}

/**
Expand Down Expand Up @@ -67,7 +67,7 @@ export class MonocleClient {
encryptedAssessment: string
): Promise<MonocleAssessment> {
try {
const response = await fetch(`${this.baseUrl}/api/v1/assessment`, {
const response = await fetch(`${this.decryptApiUrl}/api/v1/assessment`, {
method: 'POST',
headers: {
'Content-Type': 'text/plain; charset=utf-8',
Expand Down
4 changes: 2 additions & 2 deletions packages/monocle-backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
export interface MonocleOptions {
/** The secret key used for authentication with the Monocle API */
secretKey: string;
/** Optional base URL for the Monocle API. Defaults to the standard API URL if not provided */
baseUrl?: string;
/** Optional base domain for the Monocle API. Defaults to `mcl.spur.us` if not provided */
baseDomain?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import { NextMonocleProviderProps } from '../types';
export const MonocleProvider = ({
children,
publishableKey,
domain,
...rest
}: NextMonocleProviderProps) => {
const safePublishableKey =
publishableKey || process.env.NEXT_PUBLIC_MONOCLE_PUBLISHABLE_KEY || '';
const safeDomain =
domain || process.env.NEXT_PUBLIC_MONOCLE_DOMAIN || undefined;

return (
<ReactMonocleProvider publishableKey={safePublishableKey} {...rest}>
<ReactMonocleProvider
publishableKey={safePublishableKey}
domain={safeDomain}
{...rest}
>
{children}
</ReactMonocleProvider>
);
Expand Down
1 change: 1 addition & 0 deletions packages/monocle-nextjs/src/server/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const DOMAIN = process.env.NEXT_PUBLIC_MONOCLE_DOMAIN || undefined;
export const SECRET_KEY = process.env.MONOCLE_SECRET_KEY || '';
3 changes: 2 additions & 1 deletion packages/monocle-nextjs/src/server/monocleClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createMonocleClient } from '@spur.us/monocle-backend';
import { SECRET_KEY } from './constants';
import { DOMAIN, SECRET_KEY } from './constants';

const monocleClient = async () => {
return createMonocleClient({
secretKey: SECRET_KEY,
baseDomain: DOMAIN,
});
};

Expand Down
1 change: 1 addition & 0 deletions packages/monocle-react/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DOMAIN = 'mcl.spur.us';
5 changes: 3 additions & 2 deletions packages/monocle-react/src/contexts/MonocleProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { createContext, useContext, useEffect, useState } from 'react';
import { withMaxAllowedInstancesGuard } from '../utils';
import { MonocleProviderProps } from '../types';

import { DOMAIN } from '../constants';
interface MonocleContextType {
assessment: string | undefined;
refresh: () => void;
Expand All @@ -14,6 +14,7 @@ const MonocleContext = createContext<MonocleContextType | null>(null);
const MonocleProviderComponent: React.FC<MonocleProviderProps> = ({
children,
publishableKey,
domain = DOMAIN,
}) => {
const [assessment, setAssessment] = useState<string | undefined>(undefined);
const [isLoading, setIsLoading] = useState(true);
Expand All @@ -37,7 +38,7 @@ const MonocleProviderComponent: React.FC<MonocleProviderProps> = ({
const script = document.createElement('script');
script.id = '_mcl';
script.async = true;
script.src = `https://mcl.spur.us/d/mcl.js?tk=${publishableKey}`;
script.src = `https://${domain}/d/mcl.js?tk=${publishableKey}`;
script.onload = () => {
resolve();
};
Expand Down
2 changes: 2 additions & 0 deletions packages/monocle-react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ export interface MonocleProviderProps {
children: React.ReactNode;
/** The publishable key used for authentication with Monocle. */
publishableKey: string;
/** Optional base domain for the Monocle API. Defaults to `mcl.spur.us` if not provided */
domain?: string;
}
23 changes: 0 additions & 23 deletions pnpm-lock.yaml

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

1 change: 0 additions & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ catalogs:
tslib: 2.8.1
tsup: 8.4.0
typescript: 5.8.3
zx: 8.4.1
40 changes: 0 additions & 40 deletions scripts/canary.mjs

This file was deleted.