Skip to content

Commit 996e017

Browse files
author
Thomasr
committed
Private npm repository forward api
1 parent a9c15a9 commit 996e017

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

server/api-service/lowcoder-infra/src/main/java/org/lowcoder/infra/constant/NewUrl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ private NewUrl() {
3333
public static final String JS_LIBRARY = PREFIX + "/misc/js-library";
3434
public static final String MATERIAL_URL = PREFIX + "/materials";
3535
public static final String CONTACT_SYNC = PREFIX + "/sync";
36+
public static final String NPM_REGISTRY = PREFIX + "/npm";
3637
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.lowcoder.api.npm;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.lowcoder.api.framework.view.ResponseView;
6+
import org.lowcoder.api.home.SessionUserService;
7+
import org.lowcoder.api.home.SessionUserServiceImpl;
8+
import org.lowcoder.domain.organization.service.OrganizationService;
9+
import org.lowcoder.domain.organization.service.OrganizationServiceImpl;
10+
import org.lowcoder.infra.constant.NewUrl;
11+
import org.lowcoder.infra.js.NodeServerHelper;
12+
import org.lowcoder.sdk.webclient.WebClientBuildHelper;
13+
import org.springframework.core.ParameterizedTypeReference;
14+
import org.springframework.core.io.Resource;
15+
import org.springframework.http.MediaType;
16+
import org.springframework.http.ResponseEntity;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.RestController;
19+
import org.springframework.web.reactive.function.BodyInserters;
20+
import reactor.core.publisher.Mono;
21+
22+
import java.util.Map;
23+
24+
@RequiredArgsConstructor
25+
@RestController
26+
@RequestMapping(NewUrl.NPM_REGISTRY)
27+
public class PrivateNpmRegistryController implements PrivateNpmRegistryEndpoint{
28+
private final OrganizationService organizationService;
29+
private final SessionUserService sessionUserService;
30+
NodeServerHelper nodeServerHelper;
31+
32+
private static final String NPM_REGISTRY_METADATA = "npm/registry";
33+
private static final String NPM_REGISTRY_ASSET = "npm/package";
34+
35+
@Override
36+
public Mono<ResponseEntity<Resource>> getNpmPackageMeta(String name) {
37+
return forwardToNodeService(name, NPM_REGISTRY_METADATA);
38+
}
39+
40+
@Override
41+
public Mono<ResponseEntity<Resource>> getNpmPackageAsset(String path) {
42+
return forwardToNodeService(path, NPM_REGISTRY_ASSET);
43+
}
44+
45+
@NotNull
46+
private Mono<ResponseEntity<Resource>> forwardToNodeService(String path, String prefix) {
47+
return sessionUserService.getVisitorOrgMemberCache().flatMap(orgMember -> organizationService.getOrgCommonSettings(orgMember.getOrgId()).flatMap(organizationCommonSettings -> {
48+
Map<String, Object> config = Map.of("npmRegistries", organizationCommonSettings.get("npmRegistries"), "workspaceId", orgMember.getOrgId());
49+
return WebClientBuildHelper.builder()
50+
.systemProxy()
51+
.build()
52+
.post()
53+
.uri(nodeServerHelper.createUri(prefix + "/" + path))
54+
.contentType(MediaType.APPLICATION_JSON)
55+
.body(BodyInserters.fromValue(config))
56+
.retrieve().toEntity(Resource.class)
57+
.map(response -> ResponseEntity
58+
.status(response.getStatusCode())
59+
.headers(response.getHeaders())
60+
.body(response.getBody()));
61+
}));
62+
}
63+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.lowcoder.api.npm;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
import org.lowcoder.api.framework.view.ResponseView;
5+
import org.lowcoder.infra.constant.NewUrl;
6+
import org.springframework.core.io.Resource;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
import reactor.core.publisher.Mono;
13+
14+
@RestController
15+
@RequestMapping(NewUrl.NPM_REGISTRY)
16+
public interface PrivateNpmRegistryEndpoint {
17+
public static final String TAG_NPM_REGISTRY_MANAGEMENT = "Private NPM registry APIs";
18+
19+
@Operation(
20+
tags = TAG_NPM_REGISTRY_MANAGEMENT,
21+
operationId = "getNpmPackageMeta",
22+
summary = "Get NPM registry Metadata",
23+
description = "Retrieve the metadata of private NPM registry package within Lowcoder."
24+
)
25+
@GetMapping("/registry/{name}")
26+
public Mono<ResponseEntity<Resource>> getNpmPackageMeta(@PathVariable String name);
27+
28+
@Operation(
29+
tags = TAG_NPM_REGISTRY_MANAGEMENT,
30+
operationId = "getNpmPackageAsset",
31+
summary = "Get NPM registry asset",
32+
description = "Retrieve the asset of private NPM registry package within Lowcoder."
33+
)
34+
@GetMapping("/package/{path}")
35+
public Mono<ResponseEntity<Resource>> getNpmPackageAsset(@PathVariable String path);
36+
}

0 commit comments

Comments
 (0)