@youversion/platform-core 0.5.8 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +39 -0
- package/dist/index.cjs +473 -346
- package/dist/index.d.cts +106 -134
- package/dist/index.d.ts +106 -134
- package/dist/index.js +473 -343
- package/package.json +2 -1
- package/src/SignInWithYouVersionPKCE.ts +122 -0
- package/src/SignInWithYouVersionResult.ts +40 -39
- package/src/URLBuilder.ts +0 -21
- package/src/Users.ts +375 -94
- package/src/YouVersionPlatformConfiguration.ts +69 -25
- package/src/YouVersionUserInfo.ts +6 -6
- package/src/__tests__/SignInWithYouVersionPKCE.test.ts +418 -0
- package/src/__tests__/SignInWithYouVersionResult.test.ts +28 -0
- package/src/__tests__/StorageStrategy.test.ts +0 -72
- package/src/__tests__/URLBuilder.test.ts +0 -100
- package/src/__tests__/Users.test.ts +737 -0
- package/src/__tests__/YouVersionPlatformConfiguration.test.ts +192 -30
- package/src/__tests__/YouVersionUserInfo.test.ts +347 -0
- package/src/__tests__/highlights.test.ts +12 -12
- package/src/__tests__/mocks/browser.ts +90 -0
- package/src/__tests__/mocks/configuration.ts +53 -0
- package/src/__tests__/mocks/jwt.ts +93 -0
- package/src/__tests__/mocks/tokens.ts +69 -0
- package/src/index.ts +0 -3
- package/src/types/auth.ts +1 -0
- package/tsconfig.build.json +1 -1
- package/tsconfig.json +1 -1
- package/src/AuthenticationStrategy.ts +0 -78
- package/src/WebAuthenticationStrategy.ts +0 -127
- package/src/__tests__/authentication.test.ts +0 -185
- package/src/authentication.ts +0 -27
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @vitest-environment jsdom
|
|
3
|
-
*/
|
|
4
|
-
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
5
|
-
import { ApiClient } from '../client';
|
|
6
|
-
import { AuthClient } from '../authentication';
|
|
7
|
-
import { WebAuthenticationStrategy } from '../WebAuthenticationStrategy';
|
|
8
|
-
|
|
9
|
-
describe('AuthClient', () => {
|
|
10
|
-
let apiClient: ApiClient;
|
|
11
|
-
let authClient: AuthClient;
|
|
12
|
-
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
global.fetch = vi.fn();
|
|
15
|
-
apiClient = new ApiClient({
|
|
16
|
-
appKey: 'test-app-key',
|
|
17
|
-
apiHost: process.env.YVP_API_HOST || '',
|
|
18
|
-
});
|
|
19
|
-
authClient = new AuthClient(apiClient);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
afterEach(() => {
|
|
23
|
-
vi.clearAllMocks();
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
describe('getUser', () => {
|
|
27
|
-
it('should fetch user information', async () => {
|
|
28
|
-
const mockUser = {
|
|
29
|
-
id: '123',
|
|
30
|
-
first_name: 'John',
|
|
31
|
-
last_name: 'Doe',
|
|
32
|
-
avatar_url: 'https://example.com/avatar.jpg',
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
vi.mocked(global.fetch).mockResolvedValueOnce({
|
|
36
|
-
ok: true,
|
|
37
|
-
json: () => Promise.resolve(mockUser),
|
|
38
|
-
headers: {
|
|
39
|
-
get: vi.fn((name: string) => (name === 'content-type' ? 'application/json' : null)),
|
|
40
|
-
},
|
|
41
|
-
} as unknown as Response);
|
|
42
|
-
|
|
43
|
-
const user = await authClient.getUser('test-token');
|
|
44
|
-
|
|
45
|
-
expect(user).toEqual(mockUser);
|
|
46
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
47
|
-
expect.stringContaining('/auth/me'),
|
|
48
|
-
expect.any(Object),
|
|
49
|
-
);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe('WebAuthenticationStrategy', () => {
|
|
55
|
-
let strategy: WebAuthenticationStrategy;
|
|
56
|
-
const oldWindowLocation = window.location;
|
|
57
|
-
|
|
58
|
-
beforeEach(() => {
|
|
59
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
60
|
-
delete (window as any).location;
|
|
61
|
-
const newLocation = new URL('http://localhost:3000');
|
|
62
|
-
// Add mocked functions with proper descriptors
|
|
63
|
-
Object.defineProperty(newLocation, 'assign', {
|
|
64
|
-
configurable: true,
|
|
65
|
-
value: vi.fn(),
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
Object.defineProperty(newLocation, 'replace', {
|
|
69
|
-
configurable: true,
|
|
70
|
-
value: vi.fn(),
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// Assign the new location object to window.location
|
|
74
|
-
Object.defineProperty(window, 'location', {
|
|
75
|
-
configurable: true,
|
|
76
|
-
value: newLocation,
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
const sessionStorageMock = (() => {
|
|
80
|
-
let store: Record<string, string> = {};
|
|
81
|
-
return {
|
|
82
|
-
getItem: vi.fn((key: string) => store[key] || null),
|
|
83
|
-
setItem: vi.fn((key: string, value: string) => {
|
|
84
|
-
store[key] = value;
|
|
85
|
-
}),
|
|
86
|
-
removeItem: vi.fn((key: string) => {
|
|
87
|
-
delete store[key];
|
|
88
|
-
}),
|
|
89
|
-
clear: vi.fn(() => {
|
|
90
|
-
store = {};
|
|
91
|
-
}),
|
|
92
|
-
};
|
|
93
|
-
})();
|
|
94
|
-
|
|
95
|
-
Object.defineProperty(window, 'sessionStorage', {
|
|
96
|
-
value: sessionStorageMock,
|
|
97
|
-
writable: true,
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
window.history.replaceState = vi.fn();
|
|
101
|
-
|
|
102
|
-
strategy = new WebAuthenticationStrategy({
|
|
103
|
-
callbackPath: '/auth/callback',
|
|
104
|
-
timeout: 5000,
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
afterEach(() => {
|
|
109
|
-
Object.defineProperty(window, 'location', {
|
|
110
|
-
configurable: true,
|
|
111
|
-
value: oldWindowLocation,
|
|
112
|
-
});
|
|
113
|
-
WebAuthenticationStrategy.cleanup();
|
|
114
|
-
vi.clearAllMocks();
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
describe('authenticate', () => {
|
|
118
|
-
it('should redirect to auth URL', () => {
|
|
119
|
-
const authUrl = new URL('https://auth.youversion.com/authorize');
|
|
120
|
-
|
|
121
|
-
void strategy.authenticate(authUrl);
|
|
122
|
-
|
|
123
|
-
expect(window.location.href).toContain('auth.youversion.com');
|
|
124
|
-
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
125
|
-
expect(sessionStorage.setItem).toHaveBeenCalledWith(
|
|
126
|
-
'youversion-auth-return',
|
|
127
|
-
'http://localhost:3000/',
|
|
128
|
-
);
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
describe('handleCallback', () => {
|
|
133
|
-
it('should handle callback and return true', () => {
|
|
134
|
-
window.location.pathname = '/auth/callback';
|
|
135
|
-
window.location.href = 'http://localhost:3000/auth/callback?status=success';
|
|
136
|
-
// Add searchParams to mock location object for testing
|
|
137
|
-
Object.defineProperty(window.location, 'searchParams', {
|
|
138
|
-
value: new URLSearchParams('status=success'),
|
|
139
|
-
writable: true,
|
|
140
|
-
configurable: true,
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
const result = WebAuthenticationStrategy.handleCallback('/auth/callback');
|
|
144
|
-
|
|
145
|
-
expect(result).toBe(true);
|
|
146
|
-
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
147
|
-
expect(sessionStorage.setItem).toHaveBeenCalledWith(
|
|
148
|
-
'youversion-auth-callback',
|
|
149
|
-
expect.stringContaining('status=success'),
|
|
150
|
-
);
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it('should return false for non-callback URLs', () => {
|
|
154
|
-
window.location.pathname = '/';
|
|
155
|
-
Object.defineProperty(window.location, 'searchParams', {
|
|
156
|
-
value: new URLSearchParams(''),
|
|
157
|
-
writable: true,
|
|
158
|
-
configurable: true,
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
const result = WebAuthenticationStrategy.handleCallback('/auth/callback');
|
|
162
|
-
|
|
163
|
-
expect(result).toBe(false);
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
describe('getStoredCallback', () => {
|
|
168
|
-
it('should retrieve and remove stored callback', () => {
|
|
169
|
-
const callbackUrl = 'http://localhost:3000/auth/callback?status=success';
|
|
170
|
-
sessionStorage.setItem('youversion-auth-callback', callbackUrl);
|
|
171
|
-
|
|
172
|
-
const result = WebAuthenticationStrategy.getStoredCallback();
|
|
173
|
-
|
|
174
|
-
expect(result?.toString()).toBe(callbackUrl);
|
|
175
|
-
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
176
|
-
expect(sessionStorage.removeItem).toHaveBeenCalledWith('youversion-auth-callback');
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it('should return null if no stored callback', () => {
|
|
180
|
-
const result = WebAuthenticationStrategy.getStoredCallback();
|
|
181
|
-
|
|
182
|
-
expect(result).toBeNull();
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
});
|
package/src/authentication.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { ApiClient } from './client';
|
|
2
|
-
import type { User } from './types';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Client for authentication-related API calls.
|
|
6
|
-
*/
|
|
7
|
-
export class AuthClient {
|
|
8
|
-
private client: ApiClient;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Creates an instance of AuthClient.
|
|
12
|
-
* @param client - The ApiClient instance to use for requests.
|
|
13
|
-
*/
|
|
14
|
-
constructor(client: ApiClient) {
|
|
15
|
-
this.client = client;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Retrieves the current authenticated user.
|
|
20
|
-
*
|
|
21
|
-
* @param lat - The long access token (LAT) used for authentication.
|
|
22
|
-
* @returns A promise that resolves to the authenticated User.
|
|
23
|
-
*/
|
|
24
|
-
async getUser(lat: string): Promise<User> {
|
|
25
|
-
return this.client.get<User>(`/auth/me`, { lat: lat });
|
|
26
|
-
}
|
|
27
|
-
}
|