|
| 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 | +} |
0 commit comments