Skip to content

Commit 0e2e5da

Browse files
committed
Allow use of custom registry
1 parent fa9fb1e commit 0e2e5da

File tree

6 files changed

+59
-6
lines changed

6 files changed

+59
-6
lines changed

source/config/registry-settings.test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@ import test from 'ava';
22
import { checkValidationFailure, checkValidationSuccess } from '../test-libraries/verify-schema-validation.js';
33
import { registrySettingsSchema } from './registry-settings.js';
44

5-
test('validation succeeds when valid registry settings are given', checkValidationSuccess, {
5+
test('validation succeeds when no url is given', checkValidationSuccess, {
66
schema: registrySettingsSchema,
77
data: {
88
token: 'foo'
99
}
1010
});
1111

12+
test('validation succeeds when url is given', checkValidationSuccess, {
13+
schema: registrySettingsSchema,
14+
data: {
15+
token: 'foo',
16+
registryUrl: 'bar'
17+
}
18+
});
19+
1220
test('validation fails when a non-object value is given', checkValidationFailure, {
1321
schema: registrySettingsSchema,
1422
data: 'foo',

source/config/registry-settings.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { type Schema, struct } from '@effect/schema/Schema';
1+
import { type Schema, struct, optional } from '@effect/schema/Schema';
22
import { type NoExpand, nonEmptyStringSchema } from './base-validations.js';
33

44
const $registrySettingsSchema = struct({
5+
registryUrl: optional(nonEmptyStringSchema, { exact: true }),
56
token: nonEmptyStringSchema
67
});
78

source/packages/packtory/packtory.entry-point.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ export const { buildAndPublishAll } = packtory;
3434
export const progressBroadcastConsumer = progressBroadcaster.consumer;
3535

3636
export type { PacktoryConfig } from '../../config/config.js';
37+
export type { PublishAllResult } from '../../packtory/packtory.js';

source/packtory/packtory.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ type ConfigError = {
1414
};
1515

1616
export type PublishFailure = ConfigError | (PartialError & { type: 'partial' });
17+
export type PublishAllResult = Result<readonly PublishResult[], PublishFailure>;
1718

1819
export type Packtory = {
1920
buildAndPublishAll(
2021
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents -- we treat the config as unknown but want to provide autocompletion to the client
2122
config: PacktoryConfig | unknown,
2223
options: Options
23-
): Promise<Result<readonly PublishResult[], PublishFailure>>;
24+
): Promise<PublishAllResult>;
2425
};
2526

2627
type PacktoryDependencies = {

source/publisher/registry-client.test.ts

+42-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,25 @@ test('publishPackage() calls npm publish function with the given manifest and ta
5050
t.deepEqual(publish.firstCall.args, [
5151
{ name: 'the-name', version: 'the-version' },
5252
tarData,
53-
{ defaultTag: 'latest', forceAuth: { alwaysAuth: true, token: 'the-token' } }
53+
{ defaultTag: 'latest', forceAuth: { alwaysAuth: true, token: 'the-token' }, registry: undefined }
54+
]);
55+
});
56+
57+
test('publishPackage() calls npm publish function with custom registry', async (t) => {
58+
const publish = fake.resolves(undefined);
59+
const registryClient = registryClientFactory({ publish });
60+
const tarData = Buffer.from([1, 2, 3, 4]);
61+
62+
await registryClient.publishPackage({ name: 'the-name', version: 'the-version' }, tarData, {
63+
token: 'the-token',
64+
registryUrl: 'the-url'
65+
});
66+
67+
t.is(publish.callCount, 1);
68+
t.deepEqual(publish.firstCall.args, [
69+
{ name: 'the-name', version: 'the-version' },
70+
tarData,
71+
{ defaultTag: 'latest', forceAuth: { alwaysAuth: true, token: 'the-token' }, registry: 'the-url' }
5472
]);
5573
});
5674

@@ -69,7 +87,29 @@ test('fetchLatestVersion() fetches the correct package endpoint with the correct
6987
'/the-name',
7088
{
7189
forceAuth: { alwaysAuth: true, token: 'the-token' },
72-
headers: { accept: 'application/vnd.npm.install-v1+json' }
90+
headers: { accept: 'application/vnd.npm.install-v1+json' },
91+
registry: undefined
92+
}
93+
]);
94+
});
95+
96+
test('fetchLatestVersion() fetches the correct package endpoint with the correct authentication settings and headers when using a custom registry', async (t) => {
97+
const npmFetchJson = fake.resolves({
98+
name: '',
99+
'dist-tags': { latest: '1' },
100+
versions: { 1: { dist: { shasum: '', tarball: '' } } }
101+
});
102+
const registryClient = registryClientFactory({ npmFetchJson });
103+
104+
await registryClient.fetchLatestVersion('the-name', { token: 'the-token', registryUrl: 'the-url' });
105+
106+
t.is(npmFetchJson.callCount, 1);
107+
t.deepEqual(npmFetchJson.firstCall.args, [
108+
'/the-name',
109+
{
110+
forceAuth: { alwaysAuth: true, token: 'the-token' },
111+
headers: { accept: 'application/vnd.npm.install-v1+json' },
112+
registry: 'the-url'
73113
}
74114
]);
75115
});

source/publisher/registry-client.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export function createRegistryClient(dependencies: Readonly<RegistryClientDepend
7474
alwaysAuth: true,
7575
token: registrySettings.token
7676
},
77+
registry: registrySettings.registryUrl,
7778
headers: { accept: acceptHeaderForFetchingAbbreviatedResponse }
7879
});
7980
}
@@ -113,7 +114,8 @@ export function createRegistryClient(dependencies: Readonly<RegistryClientDepend
113114
forceAuth: {
114115
alwaysAuth: true,
115116
token: registrySettings.token
116-
}
117+
},
118+
registry: registrySettings.registryUrl
117119
});
118120
},
119121

0 commit comments

Comments
 (0)